Share via

Word Complete the whole Document

SteveD 430 Reputation points
2026-04-27T21:45:52.61+00:00

Hello from Steve

Can the below script please to the whole document as it stops after finding the first instance.

Sub Horse_Above_10()

With Selection.Find

    .ClearFormatting

    .Text = "\) [0-9]{2,} [A-Z]"   ' wildcard pattern

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = True                 ' <-- enable formatting filter

    .Font.Size = 12                ' <-- restrict to 12 pt only

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

If Selection.Find.Execute Then

    Selection.MoveLeft Unit:=wdCharacter, Count:=1

    Selection.Delete

    Selection.TypeText Text:=" "

    Selection.MoveDown Unit:=wdLine, Count:=1

End If
```End Sub

Developer technologies | Visual Basic for Applications

Answer accepted by question author

  1. Q&A Assist
    2026-04-27T21:46:31.97+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Use a loop around the Find.Execute call so that the code repeats for every match instead of stopping after the first one.

    Example adapting the existing macro:

    Sub Horse_Above_10()
    
        With Selection.Find
            .ClearFormatting
            .Text = "\) [0-9]{2,} [A-Z]"   ' wildcard pattern
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = True                 ' enable formatting filter
            .Font.Size = 12                ' restrict to 12 pt only
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
        End With
    
        Do While Selection.Find.Execute
            Selection.MoveLeft Unit:=wdCharacter, Count:=1
            Selection.Delete
            Selection.TypeText Text:=" "
            Selection.MoveDown Unit:=wdLine, Count:=1
        Loop
    
    End Sub
    

    Do While Selection.Find.Execute keeps running the body of the loop for each occurrence that matches the wildcard pattern, so the whole document is processed instead of only the first match.

    If the search should continue from the beginning after reaching the end of the document, set .Wrap = wdFindContinue instead of wdFindStop.


    References:

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.