如何用vba设置word的页码格式?

如下图所示为设置word的页码格式的对话框:

因为页眉页脚中可以插入不止1个页码,所以要在vba中设置页码的格式,首先要先获取页眉页脚对象HeaderFooter,然后用PageNumbers对象设置页码格式。

页码的格式一般包括编号格式NumberStyle 、是否包含章节号IncludeChapterNumber起始数字、是否在新的节重新编号RestartNumberingAtSection等。

以下代码可以设置word文档中的各个节中的不同页眉页脚的页码格式:

Sub QQ1722187970()
    Dim oSection As Section
    Dim oHF As HeaderFooter
    For Each oSection In Word.ActiveDocument.Sections
        With oSection
            With .PageSetup
            '首页不同
            .DifferentFirstPageHeaderFooter = True
            '奇偶页不同
            .OddAndEvenPagesHeaderFooter = True
            End With
            Set oHF = .Footers(wdHeaderFooterFirstPage)
            With oHF.PageNumbers
                .NumberStyle = wdPageNumberStyleNumberInDash
            End With
            '奇数页页码
             Set oHF = .Footers(wdHeaderFooterPrimary)
            With oHF.PageNumbers
                .NumberStyle = wdPageNumberStyleNumberInDash
            End With
            '偶数页页码
              Set oHF = .Footers(wdHeaderFooterEvenPages)
            With oHF.PageNumbers
                .NumberStyle = wdPageNumberStyleNumberInDash
            End With
        End With
    Next
End Sub
       

发表评论