1
votes

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?

1

1 Answers

2
votes

Whether page numbering restarts is controlled by Header and Footer\Page Number\Format Page numbers, the setting "Start at" (vs. "Continue from previous section"). If this is set to a number then page numbering will restart when section breaks are inserted. By default, this is "off", but it might be turned on in a template, for example.

In the object model the equivalent object is Document.Section.HeaderFooter.PageNumbers, the property RestartNumberingAtSection. Set this to False to make the numbering continuous from one section to the next. If it's sure the document has only one section this could be done for that section and any new sections will "inherit" the setting. Otherwise, check it in a loop at the same time SameAsPrevious is set to 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