如何打开Word.application.document.open visible = $ false

时间:2016-10-19 16:13:01

标签: .net powershell ms-word

我必须查看数千个寻找字符串的文档。当我这样做时,word文档会弹出屏幕,然后在删除对象后消失。

Documents.OpenNoRepairDialog方法的参数位于第13位以控制可见性。除了代码中列出的3个参数之外,我无法使用任何组合。有没有办法防止这些文件在代码运行时变得可见并将我的屏幕变成闪光灯?

$Doc = $Word.Documents.OpenNoRepairDialog($file,$false,$true)是我需要帮助的部分。尝试添加$null等其他参数时,我遇到错误。

$Word = New-Object -comobject Word.Application
$Word.Visible = $false
$word.DisplayAlerts = "wdAlertsNone"
$Doc = $Null
$DocContent = $Null
$Doc = $Word.Documents.OpenNoRepairDialog($file,$false,$true)
$DocContent = $Doc.Content.Text | Select-String -Pattern $Using:SearchStrings

If($DocContent)
{
    Write-Verbose "Match in $_ for Word" -Verbose
    Write-Output "Match in $_" | Out-File -FilePath $Using:OutputFilePath -Append
}
Else
{
    Write-Verbose "No Match" -Verbose
}

$Word.Documents.Close()
$Word.Quit()

While([System.Runtime.InteropServices.Marshal]::ReleaseComObject($DocContent)){}
While([System.Runtime.InteropServices.Marshal]::ReleaseComObject($Doc)){}
While([System.Runtime.InteropServices.Marshal]::ReleaseComObject($Word)){}

Remove-Variable -Name DocContent
Remove-Variable -Name Doc
Remove-Variable -Name Word

1 个答案:

答案 0 :(得分:0)

我没有试过这个,但我认为@JasonMArcher提出的solution可能会有所帮助。它允许按名称将参数传递给COM对象方法并跳过所有可选参数。我会尝试以下几点:

$Documents = $Word.Documents
Invoke-NamedParameter $Documents "OpenNoRepairDialog" @{"FileName"="$file";"Visible"="$false"}