################################################################################## # # # Script name: Replace-Word.ps1 # Author: goude@powershell.nu # Homepage: www.powershell.nu # # ################################################################################## param ([string]$Document, [string]$Replace, [string]$NewText, [switch]$help) function GetHelp() { $HelpText = @" DESCRIPTION: NAME: Replace-Word.ps1 Replaces Text in WOrd documents. PARAMETERS: -Document Path to Word Document (Required) -Replace OldText (Required) -NewText NewText (Required) -help Prints the HelpFile (Optional) SYNTAX: Replace-Word.ps1 -Document 'C:\Folder\Document.docx' -Replace "OldText" -NewText "NewText" Replaces "OldText" with "NewText" in the word document. Replace-Word.ps1 -help Displays the help topic for the script "@ $HelpText } function Replace-Word ([string]$Document,[string]$FindText,[string]$ReplaceText) { #Variables used to Match And Replace $ReplaceAll = 2 $FindContinue = 1 $MatchCase = $False $MatchWholeWord = $True $MatchWildcards = $False $MatchSoundsLike = $False $MatchAllWordForms = $False $Forward = $True $Wrap = $FindContinue $Format = $False $Word = New-Object -comobject Word.Application $Word.Visible = $False $OpenDoc = $Word.Documents.Open($Document) $Selection = $Word.Selection $Selection.Find.Execute( $FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll ) $OpenDoc.Close() } if ($help) { GetHelp } if ($Document -AND $Replace -AND $NewText) { Replace-Word $Document $Replace $NewText }