-1

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

我需要将模板中的占位符图像更改为 QR 码图像的脚本,QR 中的数据仅来自一组图像(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 发布服务器图像的结果。

非常感谢,

4

1 回答 1

0

最简单的方法大概是通过索引获取形状,然后在其位置添加一张新图片,然后移除原来的形状:

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 代码应该可以帮助您处理对象模型

于 2017-04-10T22:20:23.630 回答