$word = New-Object -COM "Word.Application";$word.Visible = $false;$doc = $word.Documents.Open($FullPath);$selection = $word.Selection;$section = $doc.sections.item(1);$header = $section.headers.Item(3);$FindText = "Cnnn";$MatchCase = $False;$MatchWholeWord = $False;$MatchWildcards = $False;$MatchSoundsLike = $False;$MatchAllWordForms = $False;$Forward = $True;$wdFindContinue = 1;$Wrap = $wdFindContinue;$Format = $False;$wdReplaceNone = 0;$ReplaceAll = 2;$ReplaceWith = "C" + $newString;$a = $header.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`$Wrap,$Format,$ReplaceWith, $ReplaceAll);Write-Host ("Header is: " + $header.Text);$doc.Save();$word.Quit();
您需要将搜索应用于TextBox或包含文本的任何shape的.TextFrame.TextRange.Find对象。 您可以尝试这样的事情:
If ($header.ShapeRange.Count) {ForEach ($shp in $header.ShapeRange) {If ($shp.TextFrame.HasText) {$obj = $shp.TextFrame.TextRange.Find$a = $obj.Execute($FindText,$MatchCase,$MatchWholeWord,`$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`$Wrap,$Format,$ReplaceWith,$ReplaceAll)}}}
$word = New-Object -ComObject Word.Application$doc= $word.documents.open('d:\test.docx',$false,$true)$doc.Paragraphs | where {$_.Range.Text -like '*知道*' } | foreach {$_.Range.Text}
#ERROR REPORTING ALLSet-StrictMode -Version latest$path = "c:\MORLAB"$files = Get-Childitem $path -Include *.docx,*.doc -Recurse | Where-Object { !($_.psiscontainer) }$output = "c:\wordfiletry.txt"$application = New-Object -comobject word.application$application.visible = $False$findtext = "CRHPCD01"Function getStringMatch{# Loop through all *.doc files in the $path directoryForeach ($file In $files){$document = $application.documents.open($file.FullName,$false,$true)$range = $document.content$wordFound = $range.find.execute($findText)if($wordFound){"$file.fullname has $wordfound" | Out-File $output -Append}}$document.close()$application.quit()}getStringMatch
#ERROR REPORTING ALLSet-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 = "First"$charactersAround = 30$results = @{}Function getStringMatch{# Loop through all *.doc files in the $path directoryForeach ($file In $files){$document = $application.documents.open($file.FullName,$false,$true)$range = $document.contentIf($range.Text -match ".{$($charactersAround)}$($findtext).{$($charactersAround)}"){$properties = @{File = $file.FullNameMatch = $findtextTextAround = $Matches[0]}$results += New-Object -TypeName PsCustomObject -Property $properties}}If($results){$results | Export-Csv $output -NoTypeInformation}$document.close()$application.quit()}getStringMatchimport-csv $output
最终版
$file_path = "E:\test" #文件位置$save_path = "E:\else\" #替换后的文件位置# $fullname_list = get-childitem -path $file_path | select-object {$_.fullname} | out-string# $files = Get-Childitem $file_path -Include *.docx,*.doc -Recurse | Where-Object { !($_.psiscontainer) }$files = Get-Childitem $file_path -Include *.docx,*.doc -Recurse # 获取文件夹下面的所有的Word文档类型文件# $FindText = "生育"$need_to_replace = @("生育","费用") #需要替换的词的列表$MatchCase = $False #区分大小写$MatchWholeWord = $False #全词匹配$MatchWildcards = $true #是否包含通配符$MatchSoundsLike = $False #发音相近$MatchAllWordForms = $False #显示消息框$Forward = $True$wdFindContinue = 1$Wrap = $wdFindContinue$Format = $False$wdReplaceNone = 0$ReplaceAll = 2$ReplaceWith = "***" #要替代的文本foreach ($file in $files){$word = New-Object -ComObject Word.Application #通过调用com实现对word的操作$word.Visible = $false #设置为可见或不可见$doc = $word.Documents.open($file.fullname,$false,$true) #打开文档$range = $doc.contentforeach ($FindText in $need_to_replace){$range.find.execute($FindText,$MatchCase,$MatchWholeWord,$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,$Wrap,$Format,$ReplaceWith, $ReplaceAll)#$single_fullname_str = $file | out-string#$name = $file.fullname.split("\")[-1]}echo $file.name$doc.SaveAs2($save_path + $file.name) #保存$doc.close() #关闭文档$word.quit() #退出Word}get-process *wps* | stop-process
加入了图像界面
Add-Type -AssemblyName System.Windows.FormsAdd-Type -AssemblyName System.Drawing$folderBrower1 = New-Object System.Windows.Forms.FolderBrowserDialog$folderBrower1.RootFolder = "Desktop"$folderBrower1.Description = '选择word文件夹'$null = $folderBrower1.ShowDialog()$folderBrower2 = New-Object System.Windows.Forms.FolderBrowserDialog$folderBrower2.RootFolder = "Desktop"$folderBrower2.Description = '选择另存为文件夹'$null = $folderBrower2.ShowDialog()$FileBrower = New-Object System.Windows.Forms.OpenFileDialog -Property @{InitialDirectory = [Environment]::GetFolderPath('Desktop')#Filter = 'Documents (*.docx)|*.docx|file (*.txt)|*.txt'Title = '选择需要替换的关键词清单'}$null = $FileBrower.ShowDialog()$file_path = $folderBrower1.SelectedPath #文件位置$save_path = $folderBrower2.SelectedPath #替换后的文件位置$files = Get-Childitem $file_path -Include *.docx,*.doc -Recurse # 获取文件夹下面的所有的Word文档类型文件$name_to_replace = Get-Content $FileBrower.FileName #这个txt文件存放在需要替换的关键词$MatchCase = $False #区分大小写$MatchWholeWord = $False #全词匹配$MatchWildcards = $true #是否包含通配符$MatchSoundsLike = $False #发音相近$MatchAllWordForms = $False #显示消息框$Forward = $True$wdFindContinue = 1$Wrap = $wdFindContinue$Format = $False$wdReplaceNone = 0$ReplaceAll = 2$ReplaceWith = "***" #要替代的文本foreach ($file in $files){$word = New-Object -ComObject Word.Application #通过调用com实现对word的操作$word.Visible = $false #设置为可见或不可见$doc = $word.Documents.open($file.fullname,$false,$true) #打开文档$range = $doc.contentforeach ($FindText in $name_to_replace){$range.find.execute($FindText,$MatchCase,$MatchWholeWord,$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,$Wrap,$Format,$ReplaceWith, $ReplaceAll)}echo $file.name$doc.SaveAs2($save_path + '\' + $file.name) #保存$doc.close() #关闭文档$word.quit() #退出Word}[System.Windows.Forms.MessageBox]::Show('全部文件已替换完成')get-process *wps* | stop-process
- 4~19行的代码是增加了一个文件选择和路径选择,直接调用了.NET类
 
