用Word互操作的Powershell脚本,如何防止修复对话框打开?

时间:2011-12-09 14:59:24

标签: powershell ms-word office-interop

我使用PowerShell脚本将Word文档批量转换为Pdfs。

脚本的转换部分是(如果需要,我可以粘贴整个脚本):

$word = New-Object -ComObject "word.application"

$outputFile = $outputDirectory + "\" + "myPdf.pdf" 

$doc = $word.documents.Open($inputFile, $refFalse,  $true) # open in background - No UI
$doc.SaveAs([ref]$outputFile, [ref]17) #17 is for PDF
$doc.Saved = $true
write-host "Processed $outputFile" -foregroundcolor Green 
$doc.Close()
$word.Quit()

该脚本运行良好,但某些源文档已损坏。 当Word检测到其中一个文档时,它会显示修复对话框。这会导致我的脚本被阻止,直到用户关闭对话框。

如何阻止此对话框?

[编辑] 这是对话框

的屏幕截图

Show Repairs dialog

5 个答案:

答案 0 :(得分:1)

如果将DisplayAlerts参数设置为$ false,我认为Christian可能会有效。

尝试以下内容,替换路径:

$filePath = "path\to\excel.xslx" # replace filename

# function pulled from http://stackoverflow.com/questions/5544844/how-to-call-a-complex-com-method-from-powershell
# allows for calling complex COM object's methods... I can define which arguments I want to send in
Function Invoke-NamedParameter {
    [CmdletBinding(DefaultParameterSetName = "Named")]
    param(
        [Parameter(ParameterSetName = "Named", Position = 0, Mandatory = $true)]
        [Parameter(ParameterSetName = "Positional", Position = 0, Mandatory = $true)]
        [ValidateNotNull()]
        [System.Object]$Object
        ,
        [Parameter(ParameterSetName = "Named", Position = 1, Mandatory = $true)]
        [Parameter(ParameterSetName = "Positional", Position = 1, Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]$Method
        ,
        [Parameter(ParameterSetName = "Named", Position = 2, Mandatory = $true)]
        [ValidateNotNull()]
        [Hashtable]$Parameter
        ,
        [Parameter(ParameterSetName = "Positional")]
        [Object[]]$Argument
    )

    end {  ## Just being explicit that this does not support pipelines
        if ($PSCmdlet.ParameterSetName -eq "Named") {
            ## Invoke method with parameter names
            ## Note: It is ok to use a hashtable here because the keys (parameter names) and values (args)
            ## will be output in the same order.  We don't need to worry about the order so long as
            ## all parameters have names
            $Object.GetType().InvokeMember($Method, [System.Reflection.BindingFlags]::InvokeMethod,
                $null,  ## Binder
                $Object,  ## Target
                ([Object[]]($Parameter.Values)),  ## Args
                $null,  ## Modifiers
                $null,  ## Culture
                ([String[]]($Parameter.Keys))  ## NamedParameters
            )
        } else {
            ## Invoke method without parameter names
            $Object.GetType().InvokeMember($Method, [System.Reflection.BindingFlags]::InvokeMethod,
                $null,  ## Binder
                $Object,  ## Target
                $Argument,  ## Args
                $null,  ## Modifiers
                $null,  ## Culture
                $null  ## NamedParameters
            )
        }
    }
}

# create Excel COM object, set to suppress alert boxes
$excelapp = new-object -com Excel.Application
$excelapp.displayalerts = $false

# open workbook with CorruptLoad = Repair
[void](invoke-namedparameter $excelapp.workbooks "Open" @{"Filename"=$filepath; "CorruptLoad"=2})

# save repaired file and close
$excelapp.activeworkbook.saveas($filepath)
$excelapp.quit()

答案 1 :(得分:1)

我只是看错了方向。

Open方法没有参数允许禁用此对话框,但我发现还有另一种方法:OpenNoRepairDialog

简单......只需要环顾四周

答案 2 :(得分:0)

您可以尝试打开这样的文档:

$word.documents.Open($inputFile, $refFalse,  $true, $null, $null, $null, $null, $null, $null, $null, $null, $null, $false, $null, $null, $null)

通过这种方式,你告诉Word不修复损坏的文件....但我无法测试它,我不知道你脚本中的行为。

在这里查看其他参数MSDN

答案 3 :(得分:0)

我遇到了同样的问题,我的脚本没有正确关闭文档试试这个以避免损坏你的文档:

# Create Word Object  
$wrd = new-object -com "word.application"

# Make Word Visible  
$wrd.visible = $true

# Open a document   
$doc = $wrd.documents.open("C:\silogix\silogix.doc") 

# Your work
# ...

# Stop Winword Process in a good way
$wrd.quit()
$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wrd)

答案 4 :(得分:0)

我知道现在已经过时了,但为了防止其他人,你可以尝试添加:

  

$ word.visible = false

     

$ word.DisplayAlerts =“wdAlertsNone”