howto save my word document as

Welcome Forums General PowerShell Q&A howto save my word document as

Viewing 4 reply threads
  • Author
    Posts
    • #217335
      Participant
      Topics: 28
      Replies: 67
      Points: 375
      Rank: Contributor

      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

      
      #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
      

      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

    • #217338
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      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.

    • #217344
      Participant
      Topics: 28
      Replies: 67
      Points: 375
      Rank: Contributor

      @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?

       

    • #217356
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      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.

    • #217362
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      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.

      • This reply was modified 1 month, 2 weeks ago by Doug Maurer.
Viewing 4 reply threads
  • You must be logged in to reply to this topic.