使用Powershell在Microsoft Publisher中替换文本 [英] Replacing Text in Microsoft Publisher Using Powershell
问题描述
我的人力资源团队问我是否可以帮助他们为我们所有的员工生成新的名片.他们有一个Publisher文件,我正在尝试替换文本.我已经写了所有从AD和循环机制中提取信息的部分,但是我无法使文本替换起作用.在使用从Word中找到.Execute方法.这很简单,因为我只是将方法作为参数输入,并且可以正常工作.
My HR team asked if I could help them generate new business cards for all of our employees. They have a Publisher file and I am trying to replace the text in. I've written all the portions that pull the info from AD and the looping mechanism but I cannot get the text replacement to function. I've done something like this in Microsoft Word before using the Find.Execute Method from Word. That was straightforward as I just fed the method my arguments and it worked.
This time though, I am trying to use the FindReplace Object from Publisher. I think I am misusing it but I'm not sure how. My code is below and any input would be appreciated. Sorry if this is a silly question, but I'm stil relatively new to PowerShell and .NET.
$Document = "C:\Test\testcard.pub"
$Publisher = New-Object -comobject Publisher.Application
$OpenDoc = $Publisher.Open($Document)
$OpenDoc.Find.Clear()
$OpenDoc.Find.FindText = "Jane"
$OpenDoc.Find.ReplaceWithText = "John"
$OpenDoc.Find.ReplaceScope = $pbReplaceScopeAll
$OpenDoc.Find.Execute()
$OpenDoc.Save()
$OpenDoc.Close()
$Publisher.quit()
推荐答案
我认为未定义$pbReplaceScopeAll
.即使看起来应该在文档中也是如此.该文档使用Visual Basic(一种从枚举隐式创建变量的语言).
I think $pbReplaceScopeAll
isn't defined. Even though it looks like it should in the documentation. The documentation uses Visual Basic, a language which implicitly creates variables from enumerations.
PowerShell doesn't offer this feature, so you'll have to directly reference the enumeration value you need. This might work:
$OpenDoc.Find.ReplaceScope = [Publisher.PbReplaceScope]::pbReplaceScopeAll
如果这不起作用,则它看起来像 pbReplaceScopeAll
的值为2
,因此您可以自己定义$ pbReplaceScopeAll:
If that doesn't work, it looks like the pbReplaceScopeAll
value is 2
, so you could define $pbReplaceScopeAll yourself:
$pbReplaceScopeAll = 2
## snip
$OpenDoc.Find.ReplaceScope = $pbReplaceScopeAll
这篇关于使用Powershell在Microsoft Publisher中替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!