使用Powershell在Microsoft Publisher中替换文本 [英] Replacing Text in Microsoft Publisher Using Powershell

查看:108
本文介绍了使用Powershell在Microsoft Publisher中替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的人力资源团队问我是否可以帮助他们为我们所有的员工生成新的名片.他们有一个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不提供此功能,因此您必须直接引用

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屋!

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