1. $word = New-Object -COM "Word.Application";
  2. $word.Visible = $false;
  3. $doc = $word.Documents.Open($FullPath);
  4. $selection = $word.Selection;
  5. $section = $doc.sections.item(1);
  6. $header = $section.headers.Item(3);
  7. $FindText = "Cnnn";
  8. $MatchCase = $False;
  9. $MatchWholeWord = $False;
  10. $MatchWildcards = $False;
  11. $MatchSoundsLike = $False;
  12. $MatchAllWordForms = $False;
  13. $Forward = $True;
  14. $wdFindContinue = 1;
  15. $Wrap = $wdFindContinue;
  16. $Format = $False;
  17. $wdReplaceNone = 0;
  18. $ReplaceAll = 2;
  19. $ReplaceWith = "C" + $newString;
  20. $a = $header.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
  21. $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
  22. $Wrap,$Format,$ReplaceWith, $ReplaceAll);
  23. Write-Host ("Header is: " + $header.Text);
  24. $doc.Save();
  25. $word.Quit();

您需要将搜索应用于TextBox或包含文本的任何shape的.TextFrame.TextRange.Find对象。 您可以尝试这样的事情:

  1. If ($header.ShapeRange.Count) {
  2. ForEach ($shp in $header.ShapeRange) {
  3. If ($shp.TextFrame.HasText) {
  4. $obj = $shp.TextFrame.TextRange.Find
  5. $a = $obj.Execute($FindText,$MatchCase,$MatchWholeWord,`
  6. $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
  7. $Wrap,$Format,$ReplaceWith,$ReplaceAll)
  8. }
  9. }
  10. }
  1. $word = New-Object -ComObject Word.Application
  2. $doc= $word.documents.open('d:\test.docx',$false,$true)
  3. $doc.Paragraphs | where {$_.Range.Text -like '*知道*' } | foreach {$_.Range.Text}
  1. #ERROR REPORTING ALL
  2. Set-StrictMode -Version latest
  3. $path = "c:\MORLAB"
  4. $files = Get-Childitem $path -Include *.docx,*.doc -Recurse | Where-Object { !($_.psiscontainer) }
  5. $output = "c:\wordfiletry.txt"
  6. $application = New-Object -comobject word.application
  7. $application.visible = $False
  8. $findtext = "CRHPCD01"
  9. Function getStringMatch
  10. {
  11. # Loop through all *.doc files in the $path directory
  12. Foreach ($file In $files)
  13. {
  14. $document = $application.documents.open($file.FullName,$false,$true)
  15. $range = $document.content
  16. $wordFound = $range.find.execute($findText)
  17. if($wordFound)
  18. {
  19. "$file.fullname has $wordfound" | Out-File $output -Append
  20. }
  21. }
  22. $document.close()
  23. $application.quit()
  24. }
  25. getStringMatch
  1. #ERROR REPORTING ALL
  2. Set-StrictMode -Version latest
  3. $path = "c:\Temp"
  4. $files = Get-Childitem $path -Include *.docx,*.doc -Recurse | Where-Object { !($_.psiscontainer) }
  5. $output = "c:\temp\wordfiletry.csv"
  6. $application = New-Object -comobject word.application
  7. $application.visible = $False
  8. $findtext = "First"
  9. $charactersAround = 30
  10. $results = @{}
  11. Function getStringMatch
  12. {
  13. # Loop through all *.doc files in the $path directory
  14. Foreach ($file In $files)
  15. {
  16. $document = $application.documents.open($file.FullName,$false,$true)
  17. $range = $document.content
  18. If($range.Text -match ".{$($charactersAround)}$($findtext).{$($charactersAround)}"){
  19. $properties = @{
  20. File = $file.FullName
  21. Match = $findtext
  22. TextAround = $Matches[0]
  23. }
  24. $results += New-Object -TypeName PsCustomObject -Property $properties
  25. }
  26. }
  27. If($results){
  28. $results | Export-Csv $output -NoTypeInformation
  29. }
  30. $document.close()
  31. $application.quit()
  32. }
  33. getStringMatch
  34. import-csv $output

最终版

  1. $file_path = "E:\test" #文件位置
  2. $save_path = "E:\else\" #替换后的文件位置
  3. # $fullname_list = get-childitem -path $file_path | select-object {$_.fullname} | out-string
  4. # $files = Get-Childitem $file_path -Include *.docx,*.doc -Recurse | Where-Object { !($_.psiscontainer) }
  5. $files = Get-Childitem $file_path -Include *.docx,*.doc -Recurse # 获取文件夹下面的所有的Word文档类型文件
  6. # $FindText = "生育"
  7. $need_to_replace = @("生育","费用") #需要替换的词的列表
  8. $MatchCase = $False #区分大小写
  9. $MatchWholeWord = $False #全词匹配
  10. $MatchWildcards = $true #是否包含通配符
  11. $MatchSoundsLike = $False #发音相近
  12. $MatchAllWordForms = $False #显示消息框
  13. $Forward = $True
  14. $wdFindContinue = 1
  15. $Wrap = $wdFindContinue
  16. $Format = $False
  17. $wdReplaceNone = 0
  18. $ReplaceAll = 2
  19. $ReplaceWith = "***" #要替代的文本
  20. foreach ($file in $files)
  21. {
  22. $word = New-Object -ComObject Word.Application #通过调用com实现对word的操作
  23. $word.Visible = $false #设置为可见或不可见
  24. $doc = $word.Documents.open($file.fullname,$false,$true) #打开文档
  25. $range = $doc.content
  26. foreach ($FindText in $need_to_replace)
  27. {
  28. $range.find.execute($FindText,$MatchCase,$MatchWholeWord,$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,$Wrap,$Format,$ReplaceWith, $ReplaceAll)
  29. #$single_fullname_str = $file | out-string
  30. #$name = $file.fullname.split("\")[-1]
  31. }
  32. echo $file.name
  33. $doc.SaveAs2($save_path + $file.name) #保存
  34. $doc.close() #关闭文档
  35. $word.quit() #退出Word
  36. }
  37. get-process *wps* | stop-process

加入了图像界面

  1. Add-Type -AssemblyName System.Windows.Forms
  2. Add-Type -AssemblyName System.Drawing
  3. $folderBrower1 = New-Object System.Windows.Forms.FolderBrowserDialog
  4. $folderBrower1.RootFolder = "Desktop"
  5. $folderBrower1.Description = '选择word文件夹'
  6. $null = $folderBrower1.ShowDialog()
  7. $folderBrower2 = New-Object System.Windows.Forms.FolderBrowserDialog
  8. $folderBrower2.RootFolder = "Desktop"
  9. $folderBrower2.Description = '选择另存为文件夹'
  10. $null = $folderBrower2.ShowDialog()
  11. $FileBrower = New-Object System.Windows.Forms.OpenFileDialog -Property @{
  12. InitialDirectory = [Environment]::GetFolderPath('Desktop')
  13. #Filter = 'Documents (*.docx)|*.docx|file (*.txt)|*.txt'
  14. Title = '选择需要替换的关键词清单'
  15. }
  16. $null = $FileBrower.ShowDialog()
  17. $file_path = $folderBrower1.SelectedPath #文件位置
  18. $save_path = $folderBrower2.SelectedPath #替换后的文件位置
  19. $files = Get-Childitem $file_path -Include *.docx,*.doc -Recurse # 获取文件夹下面的所有的Word文档类型文件
  20. $name_to_replace = Get-Content $FileBrower.FileName #这个txt文件存放在需要替换的关键词
  21. $MatchCase = $False #区分大小写
  22. $MatchWholeWord = $False #全词匹配
  23. $MatchWildcards = $true #是否包含通配符
  24. $MatchSoundsLike = $False #发音相近
  25. $MatchAllWordForms = $False #显示消息框
  26. $Forward = $True
  27. $wdFindContinue = 1
  28. $Wrap = $wdFindContinue
  29. $Format = $False
  30. $wdReplaceNone = 0
  31. $ReplaceAll = 2
  32. $ReplaceWith = "***" #要替代的文本
  33. foreach ($file in $files)
  34. {
  35. $word = New-Object -ComObject Word.Application #通过调用com实现对word的操作
  36. $word.Visible = $false #设置为可见或不可见
  37. $doc = $word.Documents.open($file.fullname,$false,$true) #打开文档
  38. $range = $doc.content
  39. foreach ($FindText in $name_to_replace)
  40. {
  41. $range.find.execute($FindText,$MatchCase,$MatchWholeWord,$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,$Wrap,$Format,$ReplaceWith, $ReplaceAll)
  42. }
  43. echo $file.name
  44. $doc.SaveAs2($save_path + '\' + $file.name) #保存
  45. $doc.close() #关闭文档
  46. $word.quit() #退出Word
  47. }
  48. [System.Windows.Forms.MessageBox]::Show('全部文件已替换完成')
  49. get-process *wps* | stop-process
  • 4~19行的代码是增加了一个文件选择和路径选择,直接调用了.NET类