首页 > 解决方案 > splitting word document into sections: how to control update of page numbering

问题描述

I have a macro that cuts a document into sections of one page each:

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

I can re-run the macro after editing the document and only an extended page will be split again.

Following the above code I loop over all sections and disable the 'link to previous' property in the headers and footers. Then I loop over the sections again to 'unlink' the PAGE and NUMPAGE fields, that is, to replace the fields with their actual values.

This works for some documents and doesn't for others. In a problem document, when I enter a section break (manually or via VBA), the page number on the following section jumps to 1, while in a no-problem document it does not.

How do I control automatic page number updating when adding a section break?

标签: vbams-wordsectionspage-numbering

解决方案


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

在对象模型中,等价的对象是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

推荐阅读