【问题标题】:Replacing Text in Microsoft Publisher Using Powershell使用 Powershell 替换 Microsoft Publisher 中的文本
【发布时间】:2015-10-28 21:40:30
【问题描述】:

我的人力资源团队询问我是否可以帮助他们为所有员工制作新名片。他们有一个 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()

【问题讨论】:

标签: powershell ms-office ms-publisher


【解决方案1】:

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

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

$OpenDoc.Find.ReplaceScope = [Publisher.PbReplaceScope]::pbReplaceScopeAll

如果这不起作用,it looks likepbReplaceScopeAll 的值是2,所以你可以自己定义 $pbReplaceScopeAll:

$pbReplaceScopeAll = 2
## snip
$OpenDoc.Find.ReplaceScope = $pbReplaceScopeAll

【讨论】:

  • 使用 pbReplaceScopeAll 的值 (2) 有效!试图调用成员 pbReplaceScope 没有。我的印象是只能在访问方法时使用双冒号?我错了吗?非常感谢您的帮助。我的人力资源团队将非常感激。
  • 双冒号是静态方法/属性操作符,用于访问类的静态方法和属性。枚举及其值被 PowerShell 视为静态类和属性。因此,[DayOfWeek]::Friday 可以访问枚举值。
  • [Publisher.PbReplaceScope]::pbReplaceScopeAll 可能不起作用,因为您需要完整的枚举名称,我在猜测(文档没有给出枚举的名称空间)。你可以通过 pipling $OpenDoc.FindGet-Member: $OpenDoc.Find | Get-Member 来解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 2020-10-30
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-12
相关资源
最近更新 更多