如何通过 Visual Basic 将页脚图像插入到 docx 文档中? (Mac)

How can I insert a footer image into a docx Document via Visual Basic? (Mac)

我得到了一个用 Apache POI 创建的 .docx 文档。我打开它并尝试通过执行两个宏来插入页眉和页脚图像:

Sub Header_Bild_Einfuegen()
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        ActiveDocument.Unprotect
    End If

    Dim oShape As Shape, oRange As Range
    Dim Pfad As String
    Pfad = "C:\Users\path\to\headerIcon.jpeg"

    Set oRange =     ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    Set oShape = ActiveDocument.Shapes.AddPicture(FileName:=Pfad, _
    LinkToFile:=False, SaveWithDocument:=True, Anchor:=oRange)

    oShape.Height = CentimetersToPoints(4.8)
    oShape.Width = CentimetersToPoints(21.55)
    oShape.Left = CentimetersToPoints(-2.44)
    oShape.Top = CentimetersToPoints(-1.28)
    oShape.ZOrder msoSendBehindText

End Sub

Sub Footer_Bild_Einfuegen()
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        ActiveDocument.Unprotect
    End If
    '
    Dim oShape As Shape, oRange As Range
    Dim Pfad As String
    Pfad = "C:\Users\path\to\footerIcon.jpeg"

    Set oRange =     ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
    Set oShape = ActiveDocument.Shapes.AddPicture(FileName:=Pfad, _
    LinkToFile:=False, SaveWithDocument:=True, Anchor:=oRange)

    oShape.Height = CentimetersToPoints(2.4)
    oShape.Width = CentimetersToPoints(21.55)
    oShape.Left = CentimetersToPoints(-2.44)
    oShape.ZOrder msoSendBehindText
End Sub

我的问题是: 两个图像都插入到文档的页眉中,页脚保持为空(但存在,包含文本(页眉也是如此))。 我试图改变几乎所有的东西,但最终都给我运行时错误。我什至更改了 Footer_Bild_Einfuegen() 的变量名,因为我认为它们可能出于任何原因合并了两个宏(没有运行时错误,只是没有用。结果是一样的与变量名称相同时的方式相同)。

在Windows下一切正常,但在Mac下就失败了。 我不知道这可能是由什么引起的,也许这只是 Mac-Office 版本中 VB 的实现(MS Office 2008 for Mac,MS Office 2016 也不起作用), 我不知道。

如果这个问题没有解决方案,有没有方便的方法将图像插入页脚而不必每次都手动调整它们的大小?

提前致谢,感谢每一个回答

终于找到方法了:

Sub Finalize()
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        ActiveDocument.Unprotect
    End If
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Or ActiveWindow.ActivePane.View.Type = wdMasterView Then
        ActiveWindow.ActivePane.View.SeekView = wdPageView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    ActiveDocument.PageSetup.FooterDistance = InchesToPoints(1)   

    Dim oShape As Shape, oRange As Range
    Dim Pfad As String
    Pfad = "/Path/To/footerIcon.jpeg"

    Set oRange = Selection.Range
    Set oShape = ActiveDocument.Shapes.AddPicture(fileName:=Pfad,     LinkToFile:=False, SaveWithDocument:=True, Anchor:=oRange)

    oShape.Height = CentimetersToPoints(2.2)
    oShape.Width = CentimetersToPoints(21.55)
    oShape.Left = CentimetersToPoints(-2.44)
    oShape.Top = CentimetersToPoints(0.28)
    oShape.ZOrder msoSendBehindText

    'HEADER

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader    

    Dim ohShape As Shape, ohRange As Range
    Dim hPfad As String
    hPfad = "/Path/To/headerIcon.jpeg"

    Set ohRange = Selection.Range
    Set ohShape = ActiveDocument.Shapes.AddPicture(fileName:=hPfad, LinkToFile:=False, SaveWithDocument:=True, Anchor:=ohRange)

    ohShape.Height = CentimetersToPoints(4.6)
    ohShape.Width = CentimetersToPoints(21.55)
    ohShape.Left = CentimetersToPoints(-2.44)
    ohShape.Top = CentimetersToPoints(-1.28)
    ohShape.ZOrder msoSendBehindText

    ActiveDocument.ActiveWindow.View.Type = wdPrintView

End Sub

这种方式在Windows下应该也有效。