1

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()
James
  • 1,514
  • 12
  • 30
  • Where does `$pbReplaceScopeAll` come from? What's its value? – Aaron Jensen Aug 05 '15 at 22:35
  • It is a member of the FindReplace Object. https://msdn.microsoft.com/EN-US/library/office/ff939498.aspx According to Microsoft: "The ReplaceScope property value can be one of the PbReplaceScope constants declared in the Microsoft Publisher type library." It is detailed here: https://msdn.microsoft.com/EN-US/library/office/ff939038.aspx – SuperSputnik45 Aug 05 '15 at 22:48

1 Answers1

0

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

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
Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91
  • Using the value (2) of pbReplaceScopeAll worked! Trying to call the member pbReplaceScope did not. I was under the impression that double colons can only be used when accessing methods? Am I wrong there? Thank you greatly for the help. My HR team will be very grateful. – SuperSputnik45 Aug 05 '15 at 23:03
  • The double colon is the static method/property operator, used to access static methods and properties on a class. Enumerations and their values are treated like static classes and properties by PowerShell. Hence, `[DayOfWeek]::Friday` to access an enum value. – Aaron Jensen Aug 05 '15 at 23:06
  • `[Publisher.PbReplaceScope]::pbReplaceScopeAll` probably doesn't work because you need the full enumeration name and I was guessing (the documentation doesn't give the enum's namespace). You could figure it out by pipling `$OpenDoc.Find` to `Get-Member`: `$OpenDoc.Find | Get-Member`. – Aaron Jensen Aug 05 '15 at 23:08