Word VBA: My Find.Replacement command will only find the first instance of the target. Why? It does not not go on to find further instances.
MY routine is supposed to find all text with a specified style and replace it with another style. IT only finds the first instance.
Function ExecReplaceStyle(strSourceStyle As String, strDestinationStyle As String) As Integer
On Error GoTo ErrorHandler
Dim Rng As Range
Dim ret As Integer
ExecReplaceStyle = 0
Set Rng = docActiveDoc.Range
Rng.Find.ClearFormatting
Rng.Find.Style = ActiveDocument.Styles(strSourceStyle)
Rng.Find.Replacement.ClearFormatting
Rng.Find.Replacement.Style = ActiveDocument.Styles(strDestinationStyle)
With Rng.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
'Rng.Find.Execute(Replace:=wdReplaceAll)
Rng.Select
Rng.Find.Execute Replace:=wdReplaceAll
ExecReplaceStyle = ret
Exit Function
ErrorHandler:
ExecReplaceStyle = Err.Number
ErrDescription = Err.Description
Resume Next
End Function
docActiveDoc
? I changed this toActiveDocument
and it works – sam092Option Explicit
at the top of your modules will help you avoid typos/misspellings like the one that @sam092 mentions. – David Zemens.Execute Replace:=wdReplaceAll
inside yourWith
block, although I don't think that's the immediate cause of your problem. – David Zemens