Consulting

Results 1 to 4 of 4

Thread: Linking starting page numbers to ending page numbers in a different section

  1. #1
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    56
    Location

    Linking starting page numbers to ending page numbers in a different section

    Here is what I am trying to do. Let's say I have a 6-section document, and I need the page number for section 6 to continue numbering from the end of section 4. In other words, section 5 interrupts the flow of the pages. So, if section 4 ended at page 5, and then section 5 has xx pages (numbered 1 - xx), then section 6 would need to start on page 6, picking up where section 4 left off.

    Through research and tinkering, I have figured out how to do this manually via code (see below) but the starting page number in section 6 is not linked in any fashion to section 4 so if section 4's page count changes, section 6's starting page number won't automatically update to reflect it. So, my question is, is there a way to do this via a formula in a page field in the footer?

    Here is the code to do it manually. In this scenario the code assumes when you run it you want to your current section to two sections prior. If I end up having to go this route, I'd change this such that the user has the ability to input which section they would like to continue numbering from.
    The commented out code is there just to confirm (for my own knowledge) that the method is returning the physical page numbers for each section (which it does).

    Thoughts?

    Private Sub cmdSectionNumbers_Click()
    '    Dim sectionscount As Long, myLoop As Long
    '
    '    sectionscount = ActiveDocument.Sections.Count
    '    For myLoop = 1 To sectionscount
    '        Debug.Print myLoop & " = "; ActiveDocument.Sections(myLoop).range.Information(WdInformation.wdActiveEndPageNumber) & " pages"
    '    Next myLoop
    
    
    
    
        Dim curSection As Long
        Dim prev2SectionPgs As Long
        Dim prev3SectionPgs As Long
        Dim adjustedPg As Long
    
    
        curSection = Selection.Information(wdActiveEndSectionNumber)
        If curSection < 3 Then
            MsgBox "Must have at least 3 sections in the document. Aborting operation."
            Exit Sub
        End If
        MsgBox "Current section = " & curSection
    
    
        prev2SectionPgs = ActiveDocument.Sections(curSection - 2).range.Information(WdInformation.wdActiveEndPageNumber)
        MsgBox "Section " & curSection - 2 & " page count = " & prev2SectionPgs
    
    
        prev3SectionPgs = ActiveDocument.Sections(curSection - 3).range.Information(WdInformation.wdActiveEndPageNumber)
        MsgBox "Section " & curSection - 3 & " page count = " & prev3SectionPgs
    
    
        adjustedPg = prev2SectionPgs - prev3SectionPgs + 1
        MsgBox "Adjusted page count for this section = " & adjustedPg
    
    
        With ActiveDocument.Sections(curSection).Headers(wdHeaderFooterPrimary).PageNumbers
            .NumberStyle = wdPageNumberStyleArabic
            .IncludeChapterNumber = False
            .RestartNumberingAtSection = True
            .StartingNumber = adjustedPg
        End With
    End Sub

  2. #2
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    56
    Location
    I'm guessing what I am asking for is not possible, right?

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,526
    Location
    You could unlink the header or footer of Sections 5 & 6 from the previous Sections, then insert a SET field containing a SECTIONPAGES field in Section 5, thus:
    {SET Sctn5 {SECTIONPAGES}}
    after which you can insert a formula field in Section 6 coded as:
    {={PAGE}-Sctn5 \# 0}
    The numbering will now be correct - with one important caveat: A Table of Contents will not respect the calculated Page #s.

    FWIW, your code might work better as:
    Private Sub cmdSectionNumbers_Click()
    Application.ScreenUpdating = False
    Dim SctnA As Long, SctnB As Long, Fld As Field
    SctnB = Selection.Information(wdActiveEndSectionNumber)
    If SctnB < 3 Then
      MsgBox "Selection in Section: " & SctnB & vbCr & _
      "Must choose at least the 3rd section in the document." & vbCr & _
      "Aborting operation.", vbCritical
      Exit Sub
    End If
    With ActiveDocument
      SctnA = .Sections(SctnB - 2).Range.Information(wdActiveEndAdjustedPageNumber)
      With .Sections(SctnB - 1)
        With .Headers(wdHeaderFooterPrimary)
          .LinkToPrevious = False
          For Each Fld In .Range.Fields
            Select Case Fld.Type
              Case wdFieldPage, wdFieldNumPages, wdFieldSectionPages: Fld.Delete
            End Select
          Next
        End With
      End With
      With .Sections(SctnB).Headers(wdHeaderFooterPrimary)
        .LinkToPrevious = False
        With .PageNumbers
          .NumberStyle = ActiveDocument.Sections(SctnA).Headers(wdHeaderFooterPrimary).PageNumbers.NumberStyle
          .IncludeChapterNumber = ActiveDocument.Sections(SctnA).Headers(wdHeaderFooterPrimary).PageNumbers.IncludeChapterNumber
          .RestartNumberingAtSection = True
          .StartingNumber = SctnA + 1
        End With
      End With
    End With
    Application.ScreenUpdating = True
    End Sub
    Note, in particular, the code to delete page numbering from the intermediate Section.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  4. #4
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    56
    Location
    Thank you! I will give this a try.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •