我设法找到&编辑每个单词文件。使用此代码:
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$objDoc = $objWord.Documents.Open("C:\users\stefan\test\New Microsoft Word Document.docx")
$objSelection = $objWord.Selection
$FindText = "that"
$MatchCase = $False
$MatchWholeWord = $true
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $wdFindContinue
$Format = $False
$wdReplaceNone = 0
$ReplaceWith = "this"
$wdFindContinue = 1
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith)
$objDoc.Save()
$objWord.Quit()
但我想为整个文件夹做这件事。我试图插入这样的内容:
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$list = Get-ChildItem "c:\users\stefan\test\*.*" -Include *.doc*
foreach($item in $list){
$objDoc = $objWord.Documents.Open($list.FullName,$true)
$objSelection = $objWord.Selection
$FindText = "Sara"
$MatchCase = $False
$MatchWholeWord = $true
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $wdFindContinue
$Format = $False
$wdReplaceNone = 0
$ReplaceWith = "AJMOO"
$wdFindContinue = 1
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith)
$objDoc.Save()
$objWord.Quit()
}
此外,它只更改找到的一个项目,但我想要文件中的所有项目。 感谢。
答案 0 :(得分:3)
此外,它只更改找到的一个项目,但我想要文件中的所有项目
这是因为您没有将替换范围设置为所有项目。这是您在Execute method call中未指定的下一个参数。设置一个名为$wdReplaceAll
and set it to 2的变量。然后调整您的调用以添加该变量。
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith,$wdReplaceAll)
修复了针对一个文件运行时的问题。
但我想为整个文件夹
做这件事
部分问题是您没有正确查询文件夹中的文件。 -Include
很挑剔并且在与-Recurse
合作时会工作,但无论如何你都会像-Filter
那样对待它,所以要进行调整。
$list = Get-ChildItem "c:\users\stefan\test\" -filter "*.doc*"
接下来,当你循环时,你没有使用当前的迭代,而是在调用.open()
时使用整个集合
$objDoc = $objWord.Documents.Open($list.FullName,$true)
应该是
$objDoc = $objWord.Documents.Open($item.FullName,$true)
根据你的循环定义。
现在您需要确保在退出应用程序之前关闭每个文档。现在你正在循环中退出单词。
foreach($item in $list){
#.... Stuff and things happens here.
$objDoc.Save()
$objDoc.Close()
}
$objWord.Quit()
现在您将$wrap
设置为变量$wdFindContinue
的值。首次调用时$wdFindContinue
为null,因为它在代码中未设置为几行。
$Wrap = $wdFindContinue
#...
$wdFindContinue = 1
切换这些行的顺序或直接将$wrap
设置为1.我不确定这是不正确的含义。
答案 1 :(得分:1)
谢天谢地@Matt我已经解决了我的代码。
这是一个正确的版本:
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$list = Get-ChildItem "c:\users\stefan\test\*.*" -Include *.doc*
foreach($item in $list){
$objDoc = $objWord.Documents.Open($item.FullName,$true)
$objSelection = $objWord.Selection
$wdFindContinue = 1
$FindText = "Sara"
$MatchCase = $False
$MatchWholeWord = $true
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $wdFindContinue
$Format = $False
$wdReplaceNone = 0
$ReplaceWith = "AJMOO"
$wdFindContinue = 1
$ReplaceAll = 2
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith,$ReplaceAll)
$objDoc.Save()
$objDoc.Close()
}
$objWord.Quit()