1

I'd like to format (e.g. highlight) special words in a Word document via VBA. In general this works nicely, but in this casde I need to find something like <*> (words enclosed in "<" and ">" brackets, where * is meant to be a wildcard, but the brackets are not. I tried to "escape" the brackets via backslash, but this does not work. I get Run-time error 5623: The replacement text contains a group number which is out of range.

wrdDocResults.Select
With Selection.Find
    .Text = "\<*\>"
    .Replacement.Font.Color = wdColorBlue 
    .Forward = True           
    .Wrap = wdFindStop          
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll   

Thanks for your help!

1 Answer 1

1

try this out

Sub test()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "\<*\>"
    .Replacement.Font.Color = wdColorBlue
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .