我的公司正在重塑品牌,我必须将所有文档中当前的页眉和页脚图像替换为新图像。当页面设置全部相同时,我只能让图像在所有页面上显示。当文档的奇数页和偶数页不同时,图像仅显示在奇数页页眉上。
Option Explicit
Sub ReplaceBanner()
Dim Shp, footerRng as Range, oSec as Section
Const Banner = "D:\Users\Name\Downloads\Rebranding\Header.png"
For Each oSec In ActiveDocument.Sections
Set headerRng = oSec.Headers(wdHeaderFooterPrimary).Range
oSec.PageSetup.OddAndEvenPagesHeaderFooter = False
For Each Shp In headerRng.InlineShapes
Shp.Delete
Next
For Each Shp In headerRng.ShapeRange
Shp.Delete
Next
Set Shp = ActiveDocument.Shapes.AddPicture(FileName:=Banner, LinkToFile:=False, _
SaveWithDocument:=True, Anchor:=headerRng)
With Shp
.Left = wdShapeCenter
.Height = InchesToPoints(0.76)
.Width = InchesToPoints(8.1)
.WrapFormat.Type = wdWrapInline
End With
Next
请尝试一下。
Sub ReplaceBanner()
Dim Shp, footerRng As Range, oSec As section, vFooter
Const Banner = "D:\Users\Name\Downloads\Rebranding\Header.png"
For Each oSec In ActiveDocument.Sections
For Each vFooter In Array(wdHeaderFooterPrimary, wdHeaderFooterEvenPages)
Set headerRng = oSec.Headers(vFooter).Range
For Each Shp In headerRng.InlineShapes
Shp.Delete
Next
For Each Shp In headerRng.ShapeRange
Shp.Delete
Next
Set Shp = ActiveDocument.Shapes.AddPicture(FileName:=Banner, LinkToFile:=False, _
SaveWithDocument:=True, Anchor:=headerRng)
With Shp
.Left = wdShapeCenter
.Height = InchesToPoints(0.76)
.Width = InchesToPoints(8.1)
.WrapFormat.Type = wdWrapInline
End With
Next
Next
End Sub