A family of Microsoft word processing software products for creating web, email, and print documents.
The cut is not executed because the first Find does not actually use wildcards, so .Text = "\(*\)" is treated as literal characters instead of a wildcard pattern. As a result, rng.Find.Execute never finds anything and Selection.Cut never runs.
To make the first Find work with the pattern \(*\) (any text in parentheses), MatchWildcards must be set to True in that With rng.Find block, and the pattern must be a valid Word wildcard expression.
Key corrections:
- Enable wildcards in the first Find:
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\(*\)" ' text in parentheses
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = True ' was False
End With
- Keep the second Find as a wildcard search (it already has
.MatchWildcards = True):
With Selection.Find
.Text = "[0-9]{1,} [A-Z]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
- Use the
Range.Findpattern consistently (optional but more robust) instead of switching toSelectioninside the loop. For example:
Do While rng.Find.Execute
rng.Cut
' work with another Range here instead of Selection
rng.Collapse wdCollapseEnd
Loop
The essential fix for the cut not firing is setting .MatchWildcards = True in the first Find so that "\(*\)" is recognized as a wildcard pattern and the loop body (including Selection.Cut) is actually reached.
References: