Word will not stop and the end of document

SteveD 625 Reputation points
2026-03-29T00:18:23.1933333+00:00

Hello From Steve

Word will not stop and the end of document

What is required please for this to happen

Sub Jockeys_and_Trainers_to_Charts()

Application.ScreenUpdating = False

Do

Selection.Find.ClearFormatting

Selection.Find.Font.Size = 12

With Selection.Find

    .Text = "|"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = False

End With

Selection.Find.Execute

If Selection.Find.found Then

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

Selection.HomeKey

With Selection.Find

    .Text = "[0-9]{1,}*[A-Z]{1,}?"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.Fields.Update

strText = Selection.Text

Selection.Collapse Direction:=wdCollapseStart

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

With Selection.Find

    .Forward = False

    .Wrap = wdFindStop

    .MatchCase = True

    .MatchWholeWord = True

    .Text = strText

    .MatchWildcards = True

    .Execute

Selection.MoveRight Unit:=wdCharacter, Count:=2

Selection.Find.ClearFormatting

With Selection.Find

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

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = terue

    .MatchCase = True

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

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

Selection.Extend

With Selection.Find

    .Text = "[A-Z] [A-Z]{1,}"

    .Replacement.Text = ""

    .Forward = False

    .Wrap = wdFindStop

    .Format = True

    .MatchCase = False

    .MatchWholeWord = True

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.Copy

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

Selection.HomeKey

With Selection.Find

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

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.Fields.Update

strText = Selection.Text

Selection.Collapse Direction:=wdCollapseStart

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

With Selection.Find

    .Forward = True

    .Wrap = wdFindStop

    .MatchCase = True

    .MatchWholeWord = True

    .Text = strText

    .MatchWildcards = False

    .Execute

End With

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

Selection.Find.ClearFormatting

With Selection.Find

    .Text = "|"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = False

End With

Selection.Find.Execute

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

Selection.Paste

End With

Selection.TypeText Text:=vbTab

Selection.HomeKey

With Selection.Find

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

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = True

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.Fields.Update

strText = Selection.Text

Selection.Collapse Direction:=wdCollapseStart

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

With Selection.Find

    .Forward = False

    .Wrap = wdFindStop

    .MatchCase = True

    .MatchWholeWord = True

    .Text = strText

    .MatchWildcards = False

    .Execute

Selection.MoveRight Unit:=wdCharacter, Count:=2

Selection.HomeKey

Selection.Find.ClearFormatting

Selection.Find.Font.Size = 10.5

With Selection.Find

    .Text = "[A-Z]-[A-Z]"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = True

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.MoveRight Unit:=wdCharacter, Count:=2

With Selection.Find

    .Text = "  "

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = True

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = False

End With

Selection.Find.Execute

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

    .Text = "[A-Z]"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = True

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.Extend

With Selection.Find

    .Text = "  "

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchWildcards = False

    .MatchSoundsLike = False

    .MatchAllWordForms = False

End With

Selection.Find.Execute

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

Selection.Copy

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

Selection.HomeKey

With Selection.Find

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

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = True

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.Fields.Update

strText = Selection.Text

Selection.Collapse Direction:=wdCollapseStart

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

With Selection.Find

    .Forward = True

    .Wrap = wdFindStop

    .MatchCase = True

    .MatchWholeWord = True

    .Text = strText

    .MatchWildcards = False

    .Execute

End With

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

Selection.Find.ClearFormatting

With Selection.Find

    .Text = "|"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = False

End With

Selection.Find.Execute

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

Selection.Paste

Selection.HomeKey

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

End If

Loop While Selection.Find.found

Application.ScreenUpdating = True
```End Sub

Developer technologies | Visual Basic for Applications

1 answer

Sort by: Oldest
  1. AI answer

    2026-03-29T00:18:38.3233333+00:00

    The macro loops with Do ... Loop While Selection.Find.Found but never checks whether the selection has reached the end of the document. When Find.Execute fails at the end, Selection.Find.Found can remain True from the previous successful find, so the loop continues indefinitely.

    To make Word stop at the end of the document, a condition based on the range limits must be added and the .Execute result must be used. The pattern in the sample loop macro is:

    With ActiveDocument.Content.Find
        .ClearFormatting
        ' set criteria
        Do While .Execute(Forward:=True, Format:=True) = True
            ' ... process found text ...
        Loop
    End With
    

    Key points to apply:

    1. Use the Boolean return value of Selection.Find.Execute (or .Execute2007) directly in the Do While condition instead of relying on Selection.Find.Found at the bottom of the loop.
    2. Base the loop on a Range (ActiveDocument.Content) rather than Selection so that when Execute fails at the end of the document, the loop exits cleanly.

    For example, the outer loop can be restructured as:

    Sub Jockeys_and_Trainers_to_Charts()
        Dim rng As Range
        Application.ScreenUpdating = False
    
        Set rng = ActiveDocument.Content
        With rng.Find
            .ClearFormatting
            .Text = "|"
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = False
    
            Do While .Execute = True
                ' rng is now at the found "|"; perform processing here
                ' ...
                ' when done, move rng past the found text so the next Execute searches forward
                rng.Collapse wdCollapseEnd
            Loop
        End With
    
        Application.ScreenUpdating = True
    End Sub
    

    This structure guarantees that when the last occurrence is passed and Execute returns False, the loop terminates and the macro stops at the end of the document.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

    1 person found this answer 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.