Share via

"Word" Needs to stop at the end of the document

SteveD 430 Reputation points
2026-05-05T03:20:36.7933333+00:00

Hello from Steve

Objective needs to stop at 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

Selection.TypeParagraph

Selection.Extend

Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting

With Selection.Find

    .Text = "kg"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .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 = wdFindStop

    .Format = False

    .MatchCase = True

    .MatchWholeWord = False

    .MatchWildcards = True

    .MatchSoundsLike = False

    .MatchAllWordForms = False

End With

Selection.Find.Execute

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

Developer technologies | Visual Basic for Applications
0 comments No comments

Answer accepted by question author

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

    Use the range-based Find consistently and avoid Selection.Find, which can continue past the intended range. Also ensure the range is moved forward after each match so the loop progresses and naturally stops at the end of the document when .Wrap = wdFindStop.

    A simplified pattern that stops at 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"
            ' Do work relative to rng here, without using Selection.Find
    
            ' Example: move rng to the end of this match so the next Execute
            ' starts after the current one
            rng.Collapse Direction:=wdCollapseEnd
        Loop
    End Sub
    

    Key points:

    1. .Wrap = wdFindStop on rng.Find ensures the loop stops when the end of ActiveDocument.Content is reached.
    2. Avoid Selection.Find with its own .Wrap inside the loop; it operates independently of rng and can search beyond the intended area.
    3. After each successful rng.Find.Execute, collapse rng to its end (wdCollapseEnd) so the next search starts after the current match.

    If additional searches (for "kg" or the wildcard pattern) are needed, perform them on a Range derived from rng (e.g., Set rng2 = rng.Duplicate) and keep .Wrap = wdFindStop on those range finds as well, instead of using Selection.Find.


    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.