powershell - 替换Word文件中页眉、页脚中的特定文本和普通文本

标签 powershell ms-word header find footer

我正在尝试编写一个 PowerShell 脚本,它将 Word 文件中的一个字符串替换为另一个字符串。我需要更新超过 500 个 word 模板和文件,所以我不想手工制作。一个问题是我无法在页脚或页眉中找到文本,因为它们都是单独的并且是带有图像的表格。我设法在正常的“正文”文本中找到文本,但现在还没有替换它。这是我的查找代码。

$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

最佳答案

我在互联网上搜索过,找到了这个问题的答案。

首先你需要了解VBA。在 MS WORD 中编写以下宏并保存。

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

将上述宏添加到 MS WORD 后,转到 Powershell 并执行以下代码。

$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()

关于powershell - 替换Word文件中页眉、页脚中的特定文本和普通文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46034902/

相关文章:

tsql - T-sql 脚本中 $(美元符号)的转义序列

以 101% 的宽度和高度插入到 Word 中的 RMarkdown 图

javascript - 在 Windows 7 中使用 JavaScript/JScript 将拼写检查窗口置于前台

perl - 为什么 Perl 和 Word VBA 中的 Word 文档页数不同?

php - .ajax跨域和MySQL无法正常工作

sql-server - 单击单选按钮时调用脚本 block

powershell - 用户可以查看我的 PowerGUI 编译的 PowerShell 脚本吗?

powershell - 如何在文件更改时在 Powershell 中运行 MSBuild 任务

html - 如何正确使用H标签?

android - 如何获取 webview 响应 header ?