Given that you are looking for "Section n" that can easily be found using
wildcards.
The replacement including the section field can be replaced using the method
in the macro. The revised code which includes the means to update the
section numbers is as follows. As replace works much faster than looping the
change is almost instantaneous. Note I have used a continuous section break
and the macro pre-supposes that the document begins with Section1. It should
not be too difficult to adapt this to suit your exact requirements,
Sub SectBreak()
'Define search text
sText = "Section [0-9]{1,}"
Selection.HomeKey Unit:=wdStory
ActiveWindow.ActivePane.View.ShowAll = True
Selection.InsertBreak Type:=wdSectionBreakContinuous
Selection.TypeText Text:="Section "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="Section"
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Cut
ActiveWindow.ActivePane.View.ShowAll = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = sText
.Replacement.Text = "^c"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
'Clean up the unwanted section break at the start.
Selection.Delete Unit:=wdCharacter, Count:=1
'Update the fields
Options.UpdateFieldsAtPrint = True
Application.ScreenUpdating = False
PrintPreview = True
PrintPreview = False
ActiveDocument.ActiveWindow.View.Type = wdPrintView
Application.ScreenUpdating = True
End Sub
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><
Graham Mayor - Word MVP
My web site
www.gmayor.com
Word MVP web site
www.mvps.org/word
<>>< ><<> ><<> <>>< ><<> <>>< <>><