dbowlds
07-31-2018, 03:45 PM
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.wdActiveEnd PageNumber) & " 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).PageNumb ers
.NumberStyle = wdPageNumberStyleArabic
.IncludeChapterNumber = False
.RestartNumberingAtSection = True
.StartingNumber = adjustedPg
End With
End Sub
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.wdActiveEnd PageNumber) & " 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).PageNumb ers
.NumberStyle = wdPageNumberStyleArabic
.IncludeChapterNumber = False
.RestartNumberingAtSection = True
.StartingNumber = adjustedPg
End With
End Sub