Exception setting "Open": Cannot convert the "True" value of type "bool" to typ

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 ?

$doc is null, like I said on technet.

Really? What’s wrong with the answer you’ve got there?

https://social.technet.microsoft.com/Forums/windowsserver/en-US/803baa09-2ae2-419b-844b-2be5300e397b/exception-setting-quotopenquot-cannot-convert-the-quottruequot-value-of-type?forum=winserverpowershell#e7075fdd-133e-4562-bcdd-22f479daa812

Why don’t you answer at least if it’s not what you’ve expected?

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.