在word文档中如果要让不同的页使用不同的页眉页脚可以插入分节符,然后取消链接到前一节功能,使得每个页面都可以拥有不同的页眉页脚。
在word文档中甚至可以只为页眉设置链接到前一节,而为页脚取消链接到前一节,或者相反。也就是,页眉和页脚可以有独立的设置。
在word vba 中 HeaderFooter 对象代表页眉或页脚对象, HeaderFooter 对象的LinkToPrevious属性用于设置是否链接到前一节功能。
由于是否链接到前一节功能可以不同的节有不同的设置,所以如果要用vba为页眉页脚设置或取消设置链接到前一节功能,需要先遍历每个节Section对象。
代码如下:
Sub QQ1722187970()
Dim oWord As Word.Application
Set oWord = Word.Application
Dim oDoc As Document
Dim oSec As Section
Dim oFoot As HeaderFooter
Dim oHead As HeaderFooter
Set oDoc = oWord.ActiveDocument
With oDoc
iCount = .Sections.Count
For i = 1 To iCount
Set oSec = .Sections(i)
With oSec
'页眉
Set oHead = .Headers(wdHeaderFooterPrimary)
'页脚
Set oFoot = .Footers(wdHeaderFooterPrimary)
'页眉取消链接到前一节
oHead.LinkToPrevious = False
'页脚链接到前一节
oFoot.LinkToPrevious = True
End With
Next i
End With
End Sub


发表评论