Hi
I’ve got this script and it’s working when I have 1 word document in the directory
what I want to change is if the search word (Fedex)is found in the document that the document is saved as Fedex_original file name.docx
[pre]
#ERROR REPORTING ALL
Set-StrictMode -Version latest
$path = “c:\Temp”
$files = Get-Childitem $path -Include .docx,.doc -Recurse | Where-Object { !($_.psiscontainer) }
$output = “c:\temp\wordfiletry.csv”
$application = New-Object -comobject word.application
$application.visible = $False
$findtext = “FEdEx”
$charactersAround = 30
$results = @{}
Function getStringMatch
{
Loop through all *.doc files in the $path directory
Foreach ($file In $files)
{
$document = $application.documents.open($file.FullName,$false,$true)
$range = $document.content
If($range.Text -match “.{$($charactersAround)}$($findtext).{$($charactersAround)}”){
$properties = @{
File = $file.FullName
Match = $findtext
TextAround = $Matches[0]
}
$results += New-Object -TypeName PsCustomObject -Property $properties
}
}
If($results){
$results | Export-Csv $output -NoTypeInformation
}
$document.close()
$application.quit()
}
getStringMatch
import-csv $output
[/pre]
however when I use multiple word documents in the same directory I get this error
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named ‘op_Addition’.
At line:26 char:14
- … $results += New-Object -TypeName PsCustomObject -Property …
-
- CategoryInfo : InvalidOperation: (op_Addition:String) , RuntimeException
- FullyQualifiedErrorId : MethodNotFound
thanks for your help