如何Documents.OpenNoRepairDialog
通过 powershell 调用该方法,并提供所有参数,至少达到打开 word 文档时没有错误、对话框或宏的程度;并且受密码保护的文档将失败,并且没有用户必须关闭的弹出窗口/对话框?
尝试通过 powershell 打开 Microsoft Office Word 2013 文档是该
Documents.Open
方法的基本用途。尝试打开包含宏或密码保护的文件会导致错误或对话框,必须手动关闭。此过程会处理数十万个文件,因此任何延迟都会浪费大量时间。
我曾尝试使用该Documents.OpenNoRepairDialog
方法绕过这些对话框并打开只读且可能已损坏的文件。下面是我用来打开文件的代码示例。
# Enable Office core libraries
Add-type -AssemblyName office
# Doc open parameter array
$DOCOpen = @{}
$DOCOpen.FileName=[string]"<Full Path to File>"
$DOCOpen.ConfirmConversions=[Microsoft.Office.Core.MsoTriState]::msoFalse
$DOCOpen.ReadOnly=[Microsoft.Office.Core.MsoTriState]::msoTrue
$DOCOpen.AddToRecentFiles=[Microsoft.Office.Core.MsoTriState]::msoFalse
$DOCOpen.PasswordDocument=$Null
$DOCOpen.PasswordTemplate=$Null
$DOCOpen.Revert=[Microsoft.Office.Core.MsoTriState]::msoTrue
$DOCOpen.WritePasswordDocument=$Null
$DOCOpen.WritePasswordTemplate=$Null
$DOCOpen.Format=[Microsoft.Office.Interop.Word.WdOpenFormat]::wdOpenFormatAuto
$DOCOpen.Encoding=[Microsoft.Office.Core.MsoEncoding]::msoEncodingOEMUnitedStates
$DOCOpen.Visible=[Microsoft.Office.Core.MsoTriState]::msoFalse
$DOCOpen.OpenAndRepair=[Microsoft.Office.Core.MsoTriState]::msoFalse
$DOCOpen.DocumentDirection=[Microsoft.Office.Interop.Word.WdDocumentDirection]::wdLeftToRight
$DOCOpen.NoEncodingDialog=[Microsoft.Office.Core.MsoTriState]::msoTrue
$DOCOpen.XMLTransform=$Null
$DOCOpen.OpenConflictDocument=[Microsoft.Office.Core.MsoTriState]::msoFalse
# Create MS Office object
$appWord = New-Object -ComObject Word.Application
# Set application objecs not visible
$appWord.visible = $False
# Supress alerts or dialogs
$appWord.DisplayAlerts = "wdAlertsNone" # $wdAlertsNone #
# Supress document macros
$appWord.AutomationSecurity = "msoAutomationSecurityForceDisable"
# Word specific settings
$appWord.ScreenUpdating = $False
$appWord.DisplayRecentFiles = $False
$appWord.DisplayScrollBars = $False
$DOCDocument = $appWord.Documents.OpenNoRepairDialog($DOCOpen.FileName, $DOCOpen.ConfirmConversions, $DOCOpen.ReadOnly, $DOCOpen.AddToRecentFiles, $DOCOpen.PasswordDocument, $DOCOpen.PasswordTemplate, $DOCOpen.Revert, $DOCOpen.WritePasswordDocument, $DOCOpen.WritePasswordTemplate, $DOCOpen.Format, $DOCOpen.Encoding, $DOCOpen.Visible, $DOCOpen.OpenAndRepair, $DOCOpen.DocumentDirection, $DOCOpen.NoEncodingDialog, $DOCOpen.XMLTransform)
该过程的结果与以下内容类似:
Object reference not set to an instance of an object.
At <Full path to file>\temp_Test_PowerShell_Code.ps1:41 char:3
+ $DOCDocument = $appWord.Documents.OpenNoRepairDialog($FileRec.FileN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException
我也尝试使用该documents.open
方法:
$DOCDocument = $appWord.Documents.Open($FileRec.FileName, $DOCOpen.ConfirmConversions, $DOCOpen.ReadOnly, $DOCOpen.AddToRecentFiles, $DOCOpen.PasswordDocument, $DOCOpen.PasswordTemplate, $DOCOpen.Revert, $DOCOpen.WritePasswordDocument, $DOCOpen.WritePasswordTemplate, $DOCOpen.Format, $DOCOpen.Encoding, $DOCOpen.Visible, $DOCOpen.OpenConflictDocument, $DOCOpen.OpenAndRepair, $DOCOpen.DocumentDirection, $DOCOpen.NoEncodingDialog)
在这种情况下,我收到以下结果:
Exception setting "Open": Cannot convert the "msoTrue" value of type "MsoTriState" to type "Object".
At <Full path to file>\temp_Test_PowerShell_Code.ps1:41 char:3
+ $DOCDocument = $appWord.Documents.Open($FileRec.FileName, $DOCOpen. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : RuntimeException
根据 Visual Basic 参考打开或修复对话框以及 .NET 参考文档.OpenNoRepairDialog,该方法的参数是系统对象,而 powershell 似乎难以以预期的格式生成。
答案1
我找到了宏问题的解决方案。我添加了
$application.AutomationSecurity = "msoAutomationSecurityForceDisable"
紧接着
$application.visible = $False
然后把AutomationSecurity
它恢复原状,我添加了
$application.AutomationSecurity = "msoAutomationSecurityByUI"
就在之前
$application.quit()
带有宏的文件会根据搜索词进行解析,而不会弹出任何窗口。