1

I have to go through thousands of documents looking for a string. When I do, the word document pops up on screen and then disappears after the object is removed.

There is a parameter of the Documents.OpenNoRepairDialog method that is at position 13 to control the visibility. I cannot get any combination to work beyond the 3 parameters that are listed in the code. Is there a way to keep these documents from being visible when the code is run and turning my screen into a strobe light?

$Doc = $Word.Documents.OpenNoRepairDialog($file,$false,$true) is the part that I need help with. I get errors when trying to add further argument such as $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
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316

1 Answers1

0

I haven't tried this out, but it looks to me that the solution proposed by @JasonMArcher might be of help. It allows passing parameters to COM object methods by name and skip all optional parameters. I'd try something along the lines of the following:

$Documents = $Word.Documents
Invoke-NamedParameter $Documents "OpenNoRepairDialog" @{"FileName"="$file";"Visible"="$false"}
Community
  • 1
  • 1
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316