PDA

View Full Version : [SOLVED:] Linking starting page numbers to ending page numbers in a different section



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

dbowlds
08-12-2018, 01:20 PM
I'm guessing what I am asking for is not possible, right?

macropod
08-12-2018, 03:27 PM
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.N umberStyle
.IncludeChapterNumber = ActiveDocument.Sections(SctnA).Headers(wdHeaderFooterPrimary).PageNumbers.I ncludeChapterNumber
.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.

dbowlds
08-14-2018, 05:23 AM
Thank you! I will give this a try.