View Single Post
 
Old 02-24-2023, 06:13 AM
alex100 alex100 is offline Windows 7 64bit Office 2016
Advanced Beginner
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default Search & replace: capture wildcard groups and set a bold formatting

I would like to replace some keywords using wildcards, while also capturing the wildcard group and applying bold formatting to the matches. I have two codes so far, but neither one accomplishes both requirements.

The first code captures the group but does not set the bold formatting:

Code:
With Selection.Find
    .Text = "(wildcard_pattern_here)"
    .Replacement.Text = "original text: \1"
    .Replacement.Font.Bold = True
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute replace:=wdReplaceAll
The second code does the exact opposite - applies the bold formatting but does not insert captured groups in the replacement text:

Code:
With Selection.Find
    .Text = "(wildcard_pattern_here)"
    .Replacement.Text = "original text: \1"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    Do While .Execute
        Selection.Font.Bold = True
        Selection.TypeText Text:=.Replacement.Text
    Loop
End With
How can I achieve both requirments at the same time, please?

Thank you!

Alex
Reply With Quote