Good Afternoon
Currently i am producing a script to open up a word document in a particular file location, then to get a find and replace taking place in the document as per the find/replace strings, then to save and close the file and move onto the next one in my list
$list = gci "C:\scripts\test" -Include *.docx -Force -recurse foreach ($foo in $list) { $objWord = New-Object -ComObject word.application $objWord.Visible = $True $objDoc = $objWord.Documents.Open("$foo") $objSelection = $objWord.Selection $findtext= "Test1" $ReplaceText = "Test2" $ReplaceAll = 2 $FindContinue = 1 $MatchFuzzy = $False $MatchCase = $False $MatchPhrase = $false $MatchWholeWord = $True $MatchWildcards = $True $MatchSoundsLike = $False $MatchAllWordForms = $False $Forward = $True $Wrap = $FindContinue $Format = $False $objSelection.Find.execute( $FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll ) $objdoc.Save() $objdoc.Close() }
However currently i have 1 document in the test folder which seems to open 7 different intances of word and produces the following errors, the file is not corrupt as it states as i can open this without issue:
Exception calling "Open" with "1" argument(s): "The file appears to be corrupted."
At C:\Scripts\test Search.ps1:14 char:34
+ $objDoc = $objWord.Documents.Open <<<< ("$foo")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\test Search.ps1:36 char:31
+ $objSelection.Find.execute <<<< (
+ CategoryInfo : InvalidOperation: (execute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Save" with "0" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x8
00706BA)"
At C:\Scripts\test Search.ps1:50 char:14
+ $objdoc.Save <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Close" with "3" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x
800706BA)"
At C:\Scripts\test Search.ps1:51 char:15
+ $objdoc.Close <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Open" with "1" argument(s): "The file appears to be corrupted."
At C:\Scripts\test Search.ps1:14 char:34
+ $objDoc = $objWord.Documents.Open <<<< ("$foo")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\test Search.ps1:36 char:31
+ $objSelection.Find.execute <<<< (
+ CategoryInfo : InvalidOperation: (execute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Save" with "0" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x8
00706BA)"
At C:\Scripts\test Search.ps1:50 char:14
+ $objdoc.Save <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Close" with "3" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x
800706BA)"
At C:\Scripts\test Search.ps1:51 char:15
+ $objdoc.Close <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Open" with "1" argument(s): "The file appears to be corrupted."
At C:\Scripts\test Search.ps1:14 char:34
+ $objDoc = $objWord.Documents.Open <<<< ("$foo")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\test Search.ps1:36 char:31
+ $objSelection.Find.execute <<<< (
+ CategoryInfo : InvalidOperation: (execute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Save" with "0" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x8
00706BA)"
At C:\Scripts\test Search.ps1:50 char:14
+ $objdoc.Save <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Close" with "3" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x
800706BA)"
At C:\Scripts\test Search.ps1:51 char:15
+ $objdoc.Close <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Open" with "1" argument(s): "The file appears to be corrupted."
At C:\Scripts\test Search.ps1:14 char:34
+ $objDoc = $objWord.Documents.Open <<<< ("$foo")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\test Search.ps1:36 char:31
+ $objSelection.Find.execute <<<< (
+ CategoryInfo : InvalidOperation: (execute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Save" with "0" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x8
00706BA)"
At C:\Scripts\test Search.ps1:50 char:14
+ $objdoc.Save <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Close" with "3" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x
800706BA)"
At C:\Scripts\test Search.ps1:51 char:15
+ $objdoc.Close <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Open" with "1" argument(s): "The file appears to be corrupted."
At C:\Scripts\test Search.ps1:14 char:34
+ $objDoc = $objWord.Documents.Open <<<< ("$foo")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\test Search.ps1:36 char:31
+ $objSelection.Find.execute <<<< (
+ CategoryInfo : InvalidOperation: (execute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Save" with "0" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x8
00706BA)"
At C:\Scripts\test Search.ps1:50 char:14
+ $objdoc.Save <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Close" with "3" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x
800706BA)"
At C:\Scripts\test Search.ps1:51 char:15
+ $objdoc.Close <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Open" with "1" argument(s): "The file appears to be corrupted."
At C:\Scripts\test Search.ps1:14 char:34
+ $objDoc = $objWord.Documents.Open <<<< ("$foo")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\test Search.ps1:36 char:31
+ $objSelection.Find.execute <<<< (
+ CategoryInfo : InvalidOperation: (execute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Save" with "0" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x8
00706BA)"
At C:\Scripts\test Search.ps1:50 char:14
+ $objdoc.Save <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Close" with "3" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x
800706BA)"
At C:\Scripts\test Search.ps1:51 char:15
+ $objdoc.Close <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Open" with "1" argument(s): "The file appears to be corrupted."
At C:\Scripts\test Search.ps1:14 char:34
+ $objDoc = $objWord.Documents.Open <<<< ("$foo")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\test Search.ps1:36 char:31
+ $objSelection.Find.execute <<<< (
+ CategoryInfo : InvalidOperation: (execute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Save" with "0" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x8
00706BA)"
At C:\Scripts\test Search.ps1:50 char:14
+ $objdoc.Save <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Exception calling "Close" with "3" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x
800706BA)"
At C:\Scripts\test Search.ps1:51 char:15
+ $objdoc.Close <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException