如何替换文件夹中Word文档中所有出现的字符串 [英] How do I replace all occurrences of string in Word documents in a folder

查看:140
本文介绍了如何替换文件夹中Word文档中所有出现的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法找到了&每个字文件进行编辑.使用以下代码:

I've managed to find& edit per one word file. With this code:

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

但是我要对整个文件夹执行此操作.我试图插入这样的内容:

But I want to do it for whole folder. I've tried to insert something like this:

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

此外,它仅更改找到的一项,但我希望文件中包含所有项. 谢谢.

Also, it changes only one item that is found, but I want all items in the file. Thanks.

推荐答案

多次替换

此外,它仅更改找到的一项,但我希望文件中的所有项

Also, it changes only one item that is found, but I want all items in the file

这是因为您尚未将替换范围设置为所有项.是您未在执行方法调用.设置一个名为 $wdReplaceAll的变量并将其设置为2 .然后,您调整调用以添加该变量.

This is because you have not set the scope of the replacement to be all items. The is from the next argument you did not specify in your Execute method call. Set a variable called $wdReplaceAll and set it to 2. Then you adjust your call to add that variable.

$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, ` 
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,` 
$Wrap,$Format,$ReplaceWith,$wdReplaceAll) 

修复了针对一个文件运行时的问题.

That fixes that issue when run against one file.

但是我想对整个文件夹进行

But I want to do it for whole folder

部分问题是您没有正确查询文件夹中的文件. -Include是挑剔的,可以与-Recurse配合使用,但是无论如何您都将其视为-Filter,因此应进行调整.

The partial issue with that is your are not properly querying the folder for files. -Include is finicky and work when partnered with -Recurse however you are treating it like a -Filter anyway so adjust as such.

$list = Get-ChildItem "c:\users\stefan\test\" -filter "*.doc*"

接下来,在循环时,调用.open()

Next, when you are looping you are not using the current iteration but the whole collection when calling .open()

$objDoc = $objWord.Documents.Open($list.FullName,$true)

应该是

$objDoc = $objWord.Documents.Open($item.FullName,$true)

根据您的循环定义.

现在,您需要确保在退出应用程序前 关闭每个文档.现在,您正在退出循环内的单词.

Now you need to be sure you close each document before you quit the application. Right now you are quitting word inside the loop.

foreach($item in $list){
    #.... Stuff and things happens here.
    $objDoc.Save()
    $objDoc.Close()
}

$objWord.Quit()

变量声明和调用

现在,将$wrap设置为变量$wdFindContinue的值.第一次调用$wdFindContinue时为null,因为在代码后面没有将其设置为几行.

Variable declaration and calls

Right now you set $wrap to the value of the variable $wdFindContinue. When that is first called $wdFindContinue is null as it is not set to a few lines later in the code.

$Wrap = $wdFindContinue 
#...
$wdFindContinue = 1 

切换这些行的顺序或直接将$wrap设置为1.我不确定这是不正确的含义.

Switch the order of these lines or just set $wrap directly to 1. I am unsure of the implications of this being incorrect.

这篇关于如何替换文件夹中Word文档中所有出现的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆