使用powershell在MS office publisher中更改图像

使用powershell在MS office publisher中更改图像,powershell,ms-office,ms-publisher,Powershell,Ms Office,Ms Publisher,我正在创建一个powershell脚本,该脚本将自动生成腕带的发布者文件。腕带上有一个二维码和一些其他细节,可以识别佩戴者的身份。我目前有一个模板文件设置,一个脚本,复制这个,重命名它,并编辑页面上的一些文本 我需要的是脚本将模板中的占位符图像更改为二维码图像,二维码中的数据仅来自一定数量的图像(1800个图像中的一个),所有图像都已生成并命名,以与Powershell中使用的名称匹配 以前是否有人使用powershell在MS Publisher中更改过图像?下面是我目前拥有的代码 $Curr

我正在创建一个powershell脚本,该脚本将自动生成腕带的发布者文件。腕带上有一个二维码和一些其他细节,可以识别佩戴者的身份。我目前有一个模板文件设置,一个脚本,复制这个,重命名它,并编辑页面上的一些文本

我需要的是脚本将模板中的占位符图像更改为二维码图像,二维码中的数据仅来自一定数量的图像(1800个图像中的一个),所有图像都已生成并命名,以与Powershell中使用的名称匹配

以前是否有人使用powershell在MS Publisher中更改过图像?下面是我目前拥有的代码

$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()
模板文档中的图像当前是一个145*145像素的空白正方形,将由相应的QR码图像替换,具体取决于$CurrentMember的值。我还没有写任何东西来尝试更改图像,因为我在网上找不到任何东西,我搜索的任何东西似乎都会返回有关Azure publisher server图像的结果

非常感谢,


Rob

最简单的方法可能是通过索引获取形状,然后在其位置添加新图片,然后删除原始形状:

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
这将帮助您找到正确的索引

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

不幸的是,我现在无法使用PowerShell,但此VBA代码应该可以帮助您使用对象模型

欢迎使用堆栈溢出。查看您迄今为止尝试过的代码以及(如您所述)您当前如何操作.pub文件中的文本会很有帮助,因为这可能有助于引导人们为您找到正确的答案。由于您尚未提供自己工作的代码示例,因此很难为您提供帮助。只是让您开始:您可以为Publisher:New object-ComObject Publisher创建一个COM对象。Application==>这可能会帮助您开始使用它。抱歉,我在发布时正在工作,所以我没有带代码!我现在已经用代码snippit更新了它…如果这变得非常简单,我道歉,我大约3周前开始使用Powershell,因为我在工作中被扔到了最深处!