Share via

Word Stop at the end of document

SteveD 430 Reputation points
2026-05-05T02:58:14.1033333+00:00

Hello from Steve

Objective please to loop to end of document.

Sub Word_to_Excel_Part_One()

Selection.Find.ClearFormatting

With Selection.Find

    .Text = "F90"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindContinue

    .Format = False

    .MatchCase = True

    .MatchWholeWord = False

    .MatchWildcards = False

    .MatchSoundsLike = False

    .MatchAllWordForms = False

End With

Selection.Find.Execute

If Selection.Find.found Then

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

Selection.TypeParagraph

Selection.Extend

Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting

With Selection.Find

    .Text = "kg"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindContinue

    .Format = False

    .MatchCase = True

    .MatchWholeWord = False

    .MatchWildcards = False

    .MatchSoundsLike = False

    .MatchAllWordForms = False

End With

Selection.Find.Execute

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

With Selection.Find

    .Text = " [0-9]{1,}."

    .Replacement.Text = ""

    .Forward = False

    .Wrap = wdFindContinue

    .Format = False

    .MatchCase = True

    .MatchWholeWord = False

    .MatchWildcards = True

    .MatchSoundsLike = False

    .MatchAllWordForms = False

End With

Selection.Find.Execute
```End Sub

Developer technologies | Visual Basic for Applications
0 comments No comments

Answer accepted by question author

  1. Q&A Assist
    2026-05-05T02:58:25.4233333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Use a loop that repeatedly calls Find.Execute and stops when .Found is False. Also set Wrap = wdFindStop so the search ends at the end of the document instead of wrapping.

    A pattern based on the documented approach with Range.Find is more reliable than using Selection directly. Example that loops all occurrences of "F90" to the end of the document:

    Sub Word_to_Excel_Part_One()
        Dim rng As Range
    
        Set rng = ActiveDocument.Content
    
        With rng.Find
            .ClearFormatting
            .Text = "F90"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop   ' stop at end of document
            .Format = False
            .MatchCase = True
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
    
        Do While rng.Find.Execute
            ' rng is now at the found "F90"; perform the rest of the logic here
            ' Example: insert paragraph before F90
            rng.Collapse Direction:=wdCollapseStart
            rng.MoveStart Unit:=wdCharacter, Count:=-1
            rng.InsertParagraphBefore
    
            ' Now search for the next occurrence, starting after the current one
            rng.Collapse Direction:=wdCollapseEnd
        Loop
    End Sub
    

    Key points from the Word object model:

    • Use Range.Find when looping through multiple matches so the selection is not constantly changed and the range is redefined on each successful find.
    • Use .Forward = True to search forward through the document.
    • Use .Wrap = wdFindStop to stop at the end of the document instead of continuing from the beginning.
    • After each successful Execute, collapse the range to its end (wdCollapseEnd) before the next Execute so the search continues after the last match.

    The same pattern can be applied to the later Find calls for "kg" and the wildcard pattern; each should use a Range and a loop, with Wrap = wdFindStop, and collapse the range between iterations.


    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.