【发布时间】:2019-01-03 23:23:41
【问题描述】:
我一直在尝试使用录制的宏替换大量 Word 文档上的标题图像并替换页脚图像。但是,当我在新文档上运行宏时,它不会替换图像并从文本框中删除文本。它根本不会触及页脚图像。我过去使用过基本的录制宏,但没有替换页脚中的图像或图像。一位朋友提到可能需要 VBA 来插入图像,但我不是该语言的新手。
Sub BrandingUpdateV2()
'
' BrandingUpdateV2 Macro
'
'
ActiveDocument.Shapes.Range(Array("Text Box 2")).Select
Selection.TypeBackspace
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.MoveDown Unit:=wdLine, Count:=13
Selection.MoveUp Unit:=wdLine, Count:=3
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.MoveDown Unit:=wdLine, Count:=42
Selection.MoveUp Unit:=wdLine, Count:=10
Selection.MoveDown Unit:=wdLine, Count:=1
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.HeaderFooter.Shapes.Range(Array("Group 50")).Select
Selection.HeaderFooter.Shapes.Range(Array("Slide Number Placeholder 11" _
)).Select
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
【问题讨论】:
-
一步一步做记录。示例只是记录删除您想要的图像,停止然后检查记录。这将比立即获胜更容易。这就像约会。
-
宏记录器在访问特定页眉和页脚时是出了名的无用 - 不幸的是。至于对图片做任何事情:您向我们展示的所有代码都是选择它们。有很多用于访问页眉和页脚内容的代码示例。搜索
ActiveDocument.sections(1).Headers(wdHeaderFooterPrimary)之类的内容即可查看。 -
一旦您对如何处理页眉/页脚感到“满意”,那么您可以考虑使用图片 (
Shapes) - 但我可以告诉您,替换Shape并不简单事情。它涉及删除当前的一个,插入一个新的和重新应用所有属性(位置,大小等)。没有没有相当于UI“更改图片" VBA 中的命令。