A family of Microsoft word processing software products for creating web, email, and print documents.
Hi, li chen
This usually happens because the search range is not being moved after a match is found, so Word keeps finding the same table cell again. It is more common in tables because Word has hidden cell/end-of-row markers.
Here are some suggestions you can try:
Use a Range, not Selection, and after each match reset the search range so the next backward search starts before the found text:
Sub FindBackwardsInTables()
Dim rng As Range
Dim lastStart As Long
Set rng = ActiveDocument.Range
rng.Collapse wdCollapseEnd
With rng.Find
.ClearFormatting
.Text = "your text here"
.Forward = False
.Wrap = wdFindStop
Do While .Execute
lastStart = rng.Start
If rng.Information(wdWithInTable) Then
'do your table-related action here
End If
rng.End = lastStart
rng.Start = ActiveDocument.Content.Start
If rng.End <= rng.Start Then Exit Do
Loop
End With
End Sub
It is recommended that you also avoid wdFindContinue here, because it can wrap around and make the loop look endless. If it still sticks, add a check for the previous Start/End values and force the range back one character before continuing.
Thank you for your patience in reading, I hope this information has been helpful to you.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment."
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.