-1

I am creating a powershell script that will auto generate publisher files for wristbands. On the Wristband is a QR code and a few other details to personally identify the wearer. I currently have a template file set up, a script that copies this, renames it, and edits some of the text on the page.

What I need it the script to change the placeholder image in the template to a QR code image, the data in the QR is only every going to be from a set amount of images (one of 1800), all have been generated and named to match up with the names used in Powershell.

Has anyone changed an image in MS Publisher using powershell before? Below is the code I currently have.

$CurrentMember = "M001S001"
$CurrectDocumet = "C:\Users\Rob\Documents\DistrictCamp2017\GeneratedFiles\" + $CurrentMember + ".pub"

copy-item "C:\Users\Rob\Documents\DistrictCamp2017\TemplateWristband.pub" "C:\Users\Rob\Documents\DistrictCamp2017\GeneratedFiles"
Rename-Item "C:\Users\Rob\Documents\DistrictCamp2017\GeneratedFiles\TemplateWristband.pub" "$CurrentMember.pub"

Add-Type -AssemblyName Microsoft.Office.Interop.Publisher
$Publisher = New-Object Microsoft.Office.Interop.Publisher.ApplicationClass

$OpenDoc = $Publisher.Open("C:\Users\Rob\Documents\DistrictCamp2017\GeneratedFiles\M001S001.pub")

###Replace Barcode and text

$pbReplaceScopeAll = 2

$OpenDoc.Find.Clear()
$OpenDoc.Find.FindText = "DEFAULT"
$OpenDoc.Find.ReplaceWithText = $CurrentMember
$OpenDoc.Find.ReplaceScope = "2" #$pbReplaceScopeAll
$OpenDoc.Find.Execute() 

$OpenDoc.Save()
$OpenDoc.Close()
$Publisher.quit()

The image in the template document is currently a blank 145*145 pixel square, to be replaced by the appropriate QR code image, dependant on the value of $CurrentMember. I haven't yet written anything to try and change the image as I cannot find anything online, anything I search for seems to return results about Azure publisher server images.

Many thanks,

Rob

RobB
  • 1
  • 1
  • 1
    Welcome to Stack Overflow. It would be helpful to see the code that you have tried so far and how (as you mention) you are currently manipulating the text in a .pub file as that might help lead people to the right answer for you. – Mark Wragg Mar 31 '17 at 10:37
  • since you didn't supply a code sample of your own work so far it's hard to help you. Just to get you started : you can create a COM object for Publisher : New-Object -ComObject Publisher.Application ==> this might help you to get started with this. – bluuf Mar 31 '17 at 11:02
  • Apologies, I was at work when I posted that so I didn't have the code with me! I've now updated it with the code snippit...Apologies if this turns out to be dead simple, I started on Powershell about 3 weeks ago by being thrown into the deep end at work! – RobB Mar 31 '17 at 15:49

1 Answers1

0

The easiest way is probably to get the shape by index, then add a new picture in its place, then remove the original shape:

Sub ReplaceFirstShapeWithImage()
    Dim oPage As Page
    Dim oShape As Shape
    Dim newImage As Shape

    Set oPage = Application.ActiveDocument.ActiveView.ActivePage

    Set oShape = oPage.Shapes(1)

    ''https://msdn.microsoft.com/en-us/library/office/ff940072.aspx
    Set newImage = oPage.Shapes.AddPicture("C:\Users\johanb\Pictures\X.png", msoFalse, msoTrue, oShape.Left, oShape.Top, oShape.Width, oShape.Height)

    oShape.Delete

End Sub

This should help you find the right index

Sub GetIndexOfSelectedShape()

    If Application.Selection.ShapeRange.Count = 0 Then
        MsgBox "Please select a shape first"
        Exit Sub
    End If

    Dim oShape As Shape
    Dim oLoopShape As Shape
    Dim i As Long

    Set oShape = Application.Selection.ShapeRange(1)

    For i = 1 To oShape.Parent.Shapes.Count
        Set oLoopShape = oShape.Parent.Shapes(i)
        If oLoopShape Is oShape Then
            MsgBox oShape.Name & " has index " & i
        End If
    Next i

End Sub

Unfortunately I can't use PowerShell right now, but this VBA code should help you with the object model

Jbjstam
  • 874
  • 6
  • 13