在word文档的页眉或页脚可以添加页码。
一个页眉或页脚可以添加多个页码。
在word vba 中PageNumber 对象代表一个页码。
而PageNumbers集合对象代表了一个页眉或页脚中的所有页码。
要添加页码可以使用PageNumbers集合对象的Add方法。
以下代码可以实现给word文档添加页码,并且居中,起始页码为5。
Sub QQ1722187970()
Dim oSection As Section
Dim oHF As HeaderFooter
Debug.Print Word.ActiveDocument.Sections.Count
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Dim oPN As PageNumber
With oDoc
For Each oSection In .Sections
With oSection
With .PageSetup
'首页不同
.DifferentFirstPageHeaderFooter = False
'奇偶页不同
.OddAndEvenPagesHeaderFooter = False
End With
Set oHF = .Footers(wdHeaderFooterPrimary)
With oHF.PageNumbers
.NumberStyle = wdPageNumberStyleArabicFullWidth
不续前节 '
.RestartNumberingAtSection = True
'从5开始编号
.StartingNumber = 5
Set oPN = .Add
With oPN
.Alignment = wdAlignPageNumberCenter
End With
End With
End With
Next
End With
End Sub


发表评论