Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-29-2021, 06:35 AM
Matt C's Avatar
Matt C Matt C is offline Restarting Paragraph/Style Numbering on Each New Page of a Document Windows 10 Restarting Paragraph/Style Numbering on Each New Page of a Document Office 97-2003
Advanced Beginner
Restarting Paragraph/Style Numbering on Each New Page of a Document
 
Join Date: May 2021
Location: London, UK
Posts: 30
Matt C is on a distinguished road
Question 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:
Sub ToggleStyleNumbering()
'
Dim intResult As Integer

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



With ActiveDocument.Content.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Custom Style")
.Replacement.Style = ActiveDocument.Styles("Custom Style Numbered")
.Wrap = wdFindContinue
.Execute , , , , , , , , , , wdReplaceAll
End With

ElseIf intResult = vbNo Then

'Remove Numbers

With ActiveDocument.Content.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Custom Style Numbered")
.Replacement.Style = ActiveDocument.Styles("Custom Style")
.Wrap = wdFindContinue
.Execute , , , , , , , , , , wdReplaceAll
End With

Else
Exit Sub

End If
End Sub
Thanks again in advance.
Reply With Quote
  #2  
Old 05-29-2021, 04:27 PM
Charles Kenyon Charles Kenyon is offline Restarting Paragraph/Style Numbering on Each New Page of a Document Windows 10 Restarting Paragraph/Style Numbering on Each New Page of a Document Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,083
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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
Reply With Quote
  #3  
Old 05-30-2021, 12:41 AM
Guessed's Avatar
Guessed Guessed is offline Restarting Paragraph/Style Numbering on Each New Page of a Document Windows 10 Restarting Paragraph/Style Numbering on Each New Page of a Document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #4  
Old 05-30-2021, 04:40 AM
Matt C's Avatar
Matt C Matt C is offline Restarting Paragraph/Style Numbering on Each New Page of a Document Windows 10 Restarting Paragraph/Style Numbering on Each New Page of a Document Office 97-2003
Advanced Beginner
Restarting Paragraph/Style Numbering on Each New Page of a Document
 
Join Date: May 2021
Location: London, UK
Posts: 30
Matt C is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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.
It is unusual. To give it context:

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.
Reply With Quote
  #5  
Old 05-30-2021, 07:28 PM
Guessed's Avatar
Guessed Guessed is offline Restarting Paragraph/Style Numbering on Each New Page of a Document Windows 10 Restarting Paragraph/Style Numbering on Each New Page of a Document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #6  
Old 05-31-2021, 04:35 AM
Matt C's Avatar
Matt C Matt C is offline Restarting Paragraph/Style Numbering on Each New Page of a Document Windows 10 Restarting Paragraph/Style Numbering on Each New Page of a Document Office 97-2003
Advanced Beginner
Restarting Paragraph/Style Numbering on Each New Page of a Document
 
Join Date: May 2021
Location: London, UK
Posts: 30
Matt C is on a distinguished road
Smile

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?
Reply With Quote
  #7  
Old 05-31-2021, 04:19 PM
Guessed's Avatar
Guessed Guessed is offline Restarting Paragraph/Style Numbering on Each New Page of a Document Windows 10 Restarting Paragraph/Style Numbering on Each New Page of a Document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #8  
Old 06-01-2021, 08:48 AM
Matt C's Avatar
Matt C Matt C is offline Restarting Paragraph/Style Numbering on Each New Page of a Document Windows 10 Restarting Paragraph/Style Numbering on Each New Page of a Document Office 97-2003
Advanced Beginner
Restarting Paragraph/Style Numbering on Each New Page of a Document
 
Join Date: May 2021
Location: London, UK
Posts: 30
Matt C is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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.
That seems to tally with my testing. The code does what is needed and I can flag the potential formatting issue in the Message Box text. It's not a macro that needs to be used frequently while the document's being written. Thanks for the help.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Restarting Paragraph/Style Numbering on Each New Page of a Document 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 Paragraph/Style Numbering on Each New Page of a Document RESTARTING page numbering in a sectioned document. jp91306 Word 3 01-01-2016 12:27 AM
Restarting Paragraph/Style Numbering on Each New Page of a Document Numbering not restarting at 1 between list styles cotjoey Word 11 09-21-2014 07:54 AM
Restarting Paragraph/Style Numbering on Each New Page of a Document reset numbering to 1 by paragraph Style Helix86 Word 5 08-07-2013 10:48 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft