【问题标题】:Using powershell to change an image in MS office publisher使用 powershell 更改 MS Office Publisher 中的图像
【发布时间】:2017-08-25 13:58:18
【问题描述】:

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

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

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

$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 发布服务器图像的结果。

非常感谢,

罗伯

【问题讨论】:

  • 欢迎来到 Stack Overflow。查看您迄今为止尝试过的代码以及您当前如何(正如您提到的)操作 .pub 文件中的文本会很有帮助,因为这可能有助于引导人们为您找到正确的答案。
  • 由于到目前为止您还没有提供自己工作的代码示例,因此很难为您提供帮助。只是为了让您入门:您可以为 Publisher 创建一个 COM 对象:New-Object -ComObject Publisher.Application ==> 这可能会帮助您开始使用它。
  • 抱歉,我发帖时正在工作,所以没有随身携带代码!我现在已经用代码片段对其进行了更新......如果这很简单,我很抱歉,大约 3 周前我开始使用 Powershell,因为工作中陷入了深渊!

标签: powershell ms-office ms-publisher


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 2010-09-16
    • 2013-11-26
    • 2015-10-28
    相关资源
    最近更新 更多