Sven
Sven

Reputation: 919

Replace specific Text in Header, Footer and normal Text in Word file

I'm trying to write a PowerShell script which replaces one String with another string in word files. I need to update more than 500 word templates and files so I don't want to make it by hand. One problem is that i can't find the text in footer or header, because they are all individual and are tables with images. I manage to find the text in the normal "body" text but haven't replaced it by now. Here is my code for finding.

$path = "C:\Users\BambergerSv\Desktop\PS\Vorlagen"
$files = Get-Childitem $path -Include *dotm, *docx, *.dot, *.doc, *.DOT, *DOTM, *.DOCX, *.DOC -Recurse |
         Where-Object { !($_.PSIsContainer) }
$application = New-Object -ComObject Word.Application
$application.Visible = $true
$findtext = "www.subdomain.domain.com"

function getStringMatch {
    foreach ($file In $files) {
        #Write-Host $file.FullName
        $document = $application.Documents.Open($file.FullName, $false, $true)
        if ($document.Content.Text -match $findtext) {
            Write-Host "found text in file " $file.FullName "`n"
        }
        try {
            $application.Documents.Close()
        } catch {
            continue
            Write-Host $file.FullName "is a read only file" #if it is write protected because of the makros
        }
    }
    $application.Quit()
}

getStringMatch

Upvotes: 2

Views: 1447

Answers (1)

Ali.Asadi
Ali.Asadi

Reputation: 809

I've searched on internet.I found an answer to this question.

At first you need to understand VBA. Write the below macro in MS WORD and then save it.

Public Function CustomReplace(findValue As String, replaceValue As String) As String

 For Each myStoryRange In ActiveDocument.StoryRanges

     myStoryRange.find.Execute FindText:=findValue, Forward:=True, ReplaceWith:=replaceValue, replace:=wdReplaceAll
     While myStoryRange.find.Found
           myStoryRange.find.Execute FindText:=findValue, Forward:=True, ReplaceWith:=replaceValue, replace:=wdReplaceAll
     Wend
     While Not (myStoryRange.NextStoryRange Is Nothing)
          Set myStoryRange = myStoryRange.NextStoryRange
          myStoryRange.find.Execute FindText:=findValue, Forward:=True, ReplaceWith:=replaceValue, replace:=wdReplaceAll

          While myStoryRange.find.Found
               myStoryRange.find.Execute FindText:=findValue, Forward:=True,ReplaceWith:=replaceValue, replace:=wdReplaceAll
          Wend

     Wend
  Next myStoryRange
CustomReplace = ActiveDocument.FullName
End Function

After the above macro added to MS WORD, go to Powershell and execute the below code.

$word = New-Object -ComObject Word.Application
$word.visible=$false
$files = Get-ChildItem "C:\Users\Ali\Desktop\Test" -Filter *.docx

$find=[ref]"Hello"
$replace=[ref]"Hi"


for ($i=0; $i -lt $files.Count; $i++) {
  $filename = $files[$i].FullName 
  $doc = $word.Documents.Open($filename)

  $word.Run("CustomReplace",$find,$replace)
  $doc.Save()
  $doc.close()
  }

 $word.quit()

Upvotes: 2

Related Questions