PDA

View Full Version : Restart page numbering at section 1 only with VBA



chrscote
09-27-2017, 09:01 AM
I have a document that runs a script to set the intro pages (TOC, List of Figures, etc.) to use lowercase roman numerals. When I get to section 1, I am currently using PageNumbers.RestartNumberingAtSection and PageNumbers.StartingNumber = 1. However, when my script gets to a major section heading (single number vs. 1.2.3), the script uses Selection.InsertBreak Type:=wdSectionBreakNextPage, which is causing them to be new sections and thus restarts numbering back at 1, but I want it to continue numbering from the previous section. So, if Section 1 ends at page 7, I want Section 2 to start at page 8, instead of 1 like it is now. How do I, in VBA, allow succeeding pages simply continue with the numbering from the previous page?

Chris

macropod
09-27-2017, 03:28 PM
How about posting the code? It's impossible to give specific advice without seeing it.

chrscote
09-28-2017, 05:10 AM
Sorry 'bout that. Here's the part of the code that I have for setting page numbers:

With Selection.Find
.Text = Chr(13) + "<IntentionalBlankCheck"
.Replacement.Text = ""
.Wrap = wdFindContinue
End With

If Selection.Find.Execute = True Then
Selection.EndKey unit:=wdLine, Extend:=wdExtend
If Selection.Text = Chr(13) + ""<IntentionalBlankCheck-SectionBreak>" + Chr(13) Then
'Reset Numbering to not start numbering at section breaks
ActiveDocument.Sections(currSect).Footers(wdHeaderFooterPrimary).PageNumber s.RestartNumberingAtSection = False
'Delete the text, add Section break and check if on an even or odd page
'If on even page, then add "This Page Intentionally Left Blank."
ElseIf Selection.Text = Chr(13) + ""<IntentionalBlankCheck-Section1>" + Chr(13) Then
'Delete the text, add Section break and check for even/odd page as above

'**************Code to set Starting number to 1********************
currSect = Selection.Information(wdActiveEndSectionNumber)
ActiveDocument.Sections(currSect).Footers(wdHeaderFooterPrimary).PageNumber s.RestartNumberingAtSection = True
ActiveDocument.Sections(currSect).Footers(wdHeaderFooterPrimary).PageNumber s.StartingNumber = 1
End If


As you can see, I have code that sets the Restart Numbering variable to false [I]every[I] time I see anything other than -Section1> just to be safe, but it still keeps starting sections at page 1.

macropod
09-28-2017, 05:44 AM
That's still not a meaningful amount of code. For example, there's no context to indicate what currSect is.

And, since you're apparently trying to do something to a document based on its content via Find/Replace, it would also be helpful if you could attach a document to a post with some representative data (delete anything sensitive). You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.