使用Powershell替换Microsoft Publisher中的文本

时间:2015-08-05 22:33:02

标签: powershell ms-office ms-publisher

我的人力资源团队询问我是否可以帮助他们为所有员工生成新的名片。他们有一个Publisher文件,我正在尝试替换文本。我已经写了从AD和循环机制中提取信息的所有部分,但我无法让文本替换功能。在使用Find.Execute Method from Word.之前,我已经在Microsoft Word中做过类似的事情这很简单,因为我只是提供了我的参数方法并且它有效。

这一次,我试图使用FindReplace Object from Publisher.我认为我在滥用它,但我不确定如何。我的代码在下面,任何输入将不胜感激。很抱歉,如果这是一个愚蠢的问题,但我对PowerShell和.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()

1 个答案:

答案 0 :(得分:0)

我认为tableView未定义。即使它看起来应该在文档中。该文档使用Visual Basic,这是一种隐式地从枚举中创建变量的语言。

PowerShell不提供此功能,因此您必须直接引用所需的the enumeration value。这可能有效:

$pbReplaceScopeAll

如果这不起作用,it looks like $OpenDoc.Find.ReplaceScope = [Publisher.PbReplaceScope]::pbReplaceScopeAll 值为pbReplaceScopeAll,那么您可以自己定义$ pbReplaceScopeAll:

2