将word文档分成几部分:如何控制页面编号的更新

问题描述 投票:1回答:1

我有一个宏,将文档分成一个页面的每个部分:

Selection.HomeKey Unit:=wdStory 
While Selection.Information(wdActiveEndPageNumber) < Selection.Information(wdNumberOfPagesInDocument)

    ActiveDocument.Bookmarks("\page").Range.Select
    With Selection.Find
      .Text = "^b"
      .Forward = True ' or False
      .Wrap = wdFindStop
      .Format = False
      If .Execute Then
        ' found section break: go to next page
        Selection.GoToNext wdGoToPage
      Else
        ' found no section break: append one
        Selection.Collapse Direction:=wdCollapseEnd
        Selection.InsertBreak Type:=wdSectionBreakNextPage
      End If
    End With

Wend

我可以在编辑文档后重新运行宏,只会再次拆分扩展页面。

按照上面的代码,我循环遍历所有部分并禁用页眉和页脚中的“link to previous”属性。然后我再次遍历这些部分以“取消链接”PAGE和NUMPAGE字段,即用实际值替换字段。

这适用于某些文档而不适用于其他文档。在问题文档中,当我输入分节符(手动或通过VBA)时,以下部分中的页码跳转到1,而在无问题文档中则不会跳转到1。

添加分节符时如何控制自动页码更新?

vba ms-word sections page-numbering
1个回答
1
投票

页码编号重新启动是否由页眉和页脚\页码\格式页码控制,设置为“开始于”(与“从上一节继续”)。如果将其设置为数字,则插入分节符时将重新开始页码编号。默认情况下,这是“关闭”,但可能会在模板中打开,例如。

在对象模型中,等效对象是Document.Section.HeaderFooter.PageNumbers,属性为RestartNumberingAtSection。将其设置为False,使编号从一个部分连续到下一个部分。如果确定文档只有一个部分,则可以为该部分完成,任何新部分都将“继承”该设置。否则,在SameAsPrevious设置为False的同时在循环中检查它。

Sub TestBreakUpPages()
    Dim Doc As Word.Document
    Dim Sec As Word.Section
    Dim hdr As Word.HeaderFooter
    Dim pageNum As PageNumbers

    Set Doc = ActiveDocument
    Selection.HomeKey Unit:=wdStory
    While Selection.Information(wdActiveEndPageNumber) < Selection.Information(wdNumberOfPagesInDocument)

        Doc.Bookmarks("\page").Range.Select
        With Selection.Find
          .Text = "^b"
          .Forward = True ' or False
          .wrap = wdFindStop
          .Format = False
          If .Execute Then
            ' found section break: go to next page
            Selection.GoToNext wdGoToPage
          Else
            ' found no section break: append one
            Selection.Collapse Direction:=wdCollapseEnd
            Selection.InsertBreak Type:=wdSectionBreakNextPage
          End If
        End With

    Wend

    For Each Sec In Doc.Sections
        Set hdr = Sec.Headers(wdHeaderFooterPrimary)
        Set pageNum = hdr.PageNumbers
        If pageNum.RestartNumberingAtSection Then
           pageNum.RestartNumberingAtSection = False
        End If
        hdr.LinkToPrevious = False
    Next

    For Each Sec In Doc.Sections
       Set hdr = Sec.Headers(wdHeaderFooterPrimary)
        hdr.Range.Fields.Unlink
    Next
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.