howto save my word document as

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

You could surround the new-object command with $() but we need to talk about how you’re collecting with +=. How many documents can there be? This operation is so expensive and slow. Here’s a faster way to do the same thing you’re trying.

#Initialize as an arraylist instead of @() array

$results = New-Object system.collections.arraylist

#use the .add() method, make sure to surround the entire command with $() and to [void] the assignment as powershell will emit the index number for each addition

[void]$results.add($(New-Object -TypeName PsCustomObject -Property $properties))

I hope you find this helpful.

@doug I’m talking about 96 doc that need to checked.

how do I change the name of the doc from original.docx to fedex_original.docx? also keeping in mind that I have to go through all these 96 docs and rename the ones where Fedex is found?

 

I thought you said that it works if there is just one file in the directory.

I’ve got this script and it’s working when I have 1 word document in the directory
My apologies for misunderstanding.

The error is occurring because

  1. $results is initialized as a hashtable @{}
  2. You should surround your New-Object command with $()
For number 1 you could initialize as an array $results = @(). I’m not 100% on number 2 but I’d do it just to be safe. If the rest of the script doesn’t properly find and rename files is another topic. It is not the cause of the error you showed.