2
我正在嘗試編寫一個PowerShell腳本,它將一個字符串替換爲另一個字符串,並放在單詞文件中。我需要更新超過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