Hi all,
I am new at scripting and don’t know how to solve the error. The script worked a view times and after that I got this error.
Can some help me? What am I doing wrong?
$source = 'C:\test'
$destination = 'C:\test2'
$keyword1 = "25159"
$keyword2 = "2016"
$keyword3 = "Examenplan"
$docs = Get-ChildItem -Path $source | Where-Object {$_.Name -like '*.doc*'}
$SearchArray = @("$keyword1","$keyword2","$keyword3")
$Word = New-Object -ComObject Word.Application
$Word.Visible = $False
$Document = $Word.Documents.Open($doc.FullName,$true,$true)
$Document.Paragraphs | ForEach-Object {
foreach ($SearchText in $SearchArray)
{$_.Range.Text | Where-Object {$_ -match $SearchText}
}
}
can you post the complete error ?
js2010
May 26, 2019, 11:56pm
3
$doc is null, like I said on technet.
Olaf
May 27, 2019, 12:05am
4
So, walking your code…
$source = 'C:\test'
$destination = 'C:\test2'
$keyword1 = "25159"
$keyword2 = "2016"
$keyword3 = "Examenplan"
Get all docs in the folder - but you are not using this anywhere.
$docs = Get-ChildItem -Path $source |
Where-Object {$_.Name -like '*.doc*'}
$SearchArray = @("$keyword1","$keyword2","$keyword3")
$Word = New-Object -ComObject Word.Application
$Word.Visible = $False
More that like this is you issue. As this … $doc … is not defined anywhere in your code.
This has nothing to do with the $docs variable above (that you’d have to loop thru), because you are only asking for one $doc, that you are not passing.
$Document = $Word.Documents.Open($doc.FullName,$true,$true)
So, unless you are looping thru $docs , and select one $doc at a time, then this does nothing.
$Document.Paragraphs |
ForEach-Object {
foreach ($SearchText in $SearchArray)
{$_.Range.Text | Where-Object {$_ -match $SearchText}
}
}
So, you script may have worked, because as some point, you populated $doc manually, but not from $docs.