View Single Post
 
Old 01-02-2017, 06:00 PM
bobbyrjw bobbyrjw is offline Windows 7 32bit Office 2003
Novice
 
Join Date: Jan 2017
Posts: 2
bobbyrjw is on a distinguished road
Default Need VBA to renumber chapters

I have a 20k word story that has been edit-chopped.
I want VBA to find each instance of the word "Chapter" and add a number that increments on each iteration, or "find". and I want to change the style to heading 1.
here is the mess I have that is not working.
I didn't know if I can dispense with the replacement text, so I entered the same as the find text.

I'm not sure it is looping all the way through the doc.
I can't find the common practice to delineate code so I will do this;

[begin VBA code]

Sub Makro6()
'
' Makro6 Makro
'
'
Dim chanumb As Integer
chanumb = 1

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "chapter"
.Replacement.Text = "Chapter " & chanumb
.Style = ActiveDocument.Styles("Heading 1")
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchAllWordForms = False

End With
chanumb = chanumb + 1
Selection.Find.Execute Replace:=wdReplaceAll

End Sub

[/end VBA code]

Thank you!
Reply With Quote