-1

I am importing plain text documents from a third party system that are long numbered lists of instructions. We put these documents into Word, make alterations, and upload them back to this third party system. But when we make alterations, this results in all our lists being out of sequence and we have to go back in and renumber them manually. I'd like to do this using VBA as we have hundreds of documents that will require these alterations.

Does anyone have any suggestions for how to go about this? I was thinking about stepping through each paragraph (each instruction is a paragraph), identifying which ones begin with numbers and thus need to be relabeled (some paragraphs are not numbered steps), deleting the existing number, replacing that deleted number with the next number in sequence.

I've found a few sites with code blocks that I can likely use, but wanted to see if I'm going about this the right way or if I should be trying another strategy, like changing each paragraph to automatic numbering as I step through the paragraphs.

braX
  • 11,506
  • 5
  • 20
  • 33
  • 1
    Because you have some un-numbered paragraphs, the approach you list is probably a good one. The use of regular expressions (see [this answer](https://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops)) can help you identify / store the numbered lines so that you can change them as needed. – SmrtGrunt Jun 07 '19 at 18:11
  • 1
    Cross-posted at: https://answers.microsoft.com/en-us/msoffice/forum/all/vba-code-to-renumber-manual-numbered-list/95ccc0c6-faa0-40e5-ab52-57828cf7b884. For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184 – macropod Jun 08 '19 at 02:57

2 Answers2

1

For a simple series of numbered paragraphs that you might select, you could use code like:

Sub Demo()
With Selection.Range.ListFormat
  .ApplyNumberDefault
  .ApplyListTemplate ListTemplate:=ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:=True
End With
End Sub

Do be aware that the paragraph indenting may change and that such code will also number any unnumbered paragraphs in the selection So be careful with what you select.

macropod
  • 12,757
  • 2
  • 9
  • 21
0

This is a perfect toggle

Sub BulletNumeric()
'
' BulletNumeric Macro
'
'
Dim lfTemp As ListFormat
Dim intContinue As Integer
 
If Selection.Range.ListFormat.ListString = "1." Then
    Selection.Style = "Bullet Numbered"
Else
    
    Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdOutlineNumberGallery).ListTemplates(7), _
        ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
        DefaultListBehavior:=wdWord10ListBehavior

End If

End Sub

First time you click it, it will give you 1. Next time it will simply continue from the one before.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • Can you revisit this code and make sure it is consistently indented so it’s easier to read? I fixed the formatting but it still needs some work. – Jeremy Caney Jul 05 '20 at 02:59