【发布时间】:2018-02-12 13:33:28
【问题描述】:
我正在尝试编写一个 PowerShell 脚本,将 word 文件中的一个字符串替换为另一个字符串。我需要更新超过 500 个单词的模板和文件,所以我不想手工制作。一个问题是我在页脚或页眉中找不到文本,因为它们都是单独的并且是带有图像的表格。我设法在正常的“正文”文本中找到文本,但现在还没有替换它。这是我的查找代码。
$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
【问题讨论】:
标签: powershell ms-word header find footer