【问题标题】:How to open Word.application.document.open visible=$false如何打开 Word.application.document.open 可见=$f​​alse
【发布时间】:2017-03-01 09:42:49
【问题描述】:

我必须通过数千个文档来寻找一个字符串。当我这样做时,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

【问题讨论】:

    标签: .net powershell ms-word


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-12-29
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多