#1
|
||||
|
||||
Restarting Paragraph/Style Numbering on Each New Page of a Document
Hi, VBA Pros
I'm currently using the code below to add or remove numbering to/from a specific custom style in a document. All works perfectly but the numbering needs to restart from "1" on each new page. At the moment I'm simply scrolling through each page pressing Word's built-in "Restart Numbering" but I'm wondering if VBA can do it for me in one swift stroke. It can be an entirely separate macro if needs be. The most important bit is that I must be able to toggle the numbers on and off without any undue complications, which the existing code allows. EDIT: Just to add that "Custom Style Numbered" will not always be at the top of a page, if it makes any difference. The document contains a number of styles but this is the only one which should include optional numbering. (This is something I've been battling with for several years, by the way!) Quote:
|
#2
|
|||
|
|||
Look into the Word object model and how Word configures things.
Word Doesn't Know What a Page Is You may want to have a style that is (1) unnumbered, but (2) at the top level of a multil-level list. Set that style for Page-Break before formatting and use it to start new pages when you want. Set level 2 numbering to restart after each level one instance. How to create numbered headings or outline numbering in Ribbon Versions of Word by Shauna Kelly |
#3
|
||||
|
||||
This is unusual to number paragraphs and restart them relative to the page they sit on rather than the logical structure of the content. You would need to reset the content continuously with any changes in pagination which is a continuously repeating process.
Are you sure you don't really want Line Numbering? This can be very simple to implement and will be much more robust in restarting on each page. See How to Add Line Numbers to a Microsoft Word Document
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#4
|
||||
|
||||
Quote:
I'm the creator of a set of long-standing Word templates called "Script Smart" for writers (well, for me - but I share them with other writers too) to help format scripts for films, stage plays and radio plays correctly. This particular issue is related to the "Radio Play" template. In standard radio play script format, all dialogue cues are numbered but restart from "1" on each new page rather than being consecutive throughout the script. It's just part of the standard production process. The line numbering function would indeed solve the problem if character names appeared on a separate line to the dialogue (as with screenplays) but, in this case, they both share the first line (CHARACTER NAME [tab] Dialogue), so the style is a paragraph with hanging indent. |
#5
|
||||
|
||||
This would work but you might not like the global paragraph reset in the middle if you have paragraphs with local para formatting other than number resets.
Code:
Sub ToggleStyleNumbering2() Dim intResult As Integer, sFind As String, sReplace As String, iPage As Integer, aPar As Paragraph Dim aLT As ListTemplate intResult = MsgBox("Would you like to add numbering to Custom Style?" & vbCrLf & vbCrLf & _ "Click 'Yes' to ADD or 'No' to REMOVE", vbYesNoCancel + vbQuestion, "Add or Remove Numbering") If intResult = vbYes Then 'Add Numbers sFind = "Normal" sReplace = "List Number" ElseIf intResult = vbNo Then 'Remove Numbers sFind = "List Number" sReplace = "Normal" Else Exit Sub End If With ActiveDocument.Content.Find .ClearFormatting .Style = ActiveDocument.Styles(sFind) .Replacement.Style = ActiveDocument.Styles(sReplace) .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With ActiveDocument.Range.ParagraphFormat.Reset 'clears all paragraph format resets If sReplace = "List Number" Then Set aLT = ActiveDocument.Styles("List Number").ListTemplate For Each aPar In ActiveDocument.Range.Paragraphs If aPar.Style = "List Number" Then If aPar.Range.Information(wdActiveEndPageNumber) > iPage Then aPar.Range.ListFormat.ApplyListTemplate ListTemplate:=aPar.Range.ListFormat.ListTemplate, ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList iPage = aPar.Range.Information(wdActiveEndPageNumber) End If End If Next aPar End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#6
|
||||
|
||||
Hi Andrew
I'm seriously grateful for this! I've been tinkering around and digesting the code and understand the local-formatting issue. In fact, I discovered that the formatting issue is evident in the "old way" I was doing it, i.e. when manually restarting the numbering in a paragraph. Considering that any local paragraph formatting should be relatively limited anyway - as this is a script format with "layout rules" after all - your numbering system saves way more legwork than the minor issues it causes. One or two observations: Formatting wasn't always affected. Sometimes I would italicise a couple of lines and it stayed in place when toggling the numbering on and off, but if I italicised the whole paragraph (even ignoring the paragraph marker) then the formatting would reset. Individual words with local formatting (which is the much more likely scenario) never seem to be affected. So what's going on there? |
#7
|
||||
|
||||
Unfortunately, restarting the number IS a local format setting. That is why I reset the paragraph format to remove any already restarted paragraphs before looking for the 'current' start of each page.
I'm not sure what is going on with your italicised whole paragraph (I would expect that to be a font atttribute rather than a paragraph format) but there is a known behaviour where if more than half the paragraph is formatted one way then the whole paragraph is affected. If the reset is an issue, you can slow the macro down by disabling the full doc reset and add an Else on the innermost If section to continue the numbers if they aren't the first instance on the page. This means you reset every instance individually at the expense of speed - which might not bother you.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
||||
|
||||
Quote:
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Restarting page numbering only if Section begins with Heading 1 | kaurp | Word VBA | 5 | 11-02-2017 04:36 AM |
How can I define a numbering style restarting numbering after an outline by default | jklocker | Word | 1 | 09-26-2017 05:11 AM |
RESTARTING page numbering in a sectioned document. | jp91306 | Word | 3 | 01-01-2016 12:27 AM |
Numbering not restarting at 1 between list styles | cotjoey | Word | 11 | 09-21-2014 07:54 AM |
reset numbering to 1 by paragraph Style | Helix86 | Word | 5 | 08-07-2013 10:48 PM |