After defining a range in Word, I’m trying to delete all occurrences of a string within that range. However, the Find function instead continues to the end of the document.
I’ll illustrate the problem with a toy example. I set up a Word document containing the following text:
abcdefghij
And then use this code:
Sub test()
Dim r As Range
Set r = ThisDocument.Content
'Limit the range to four characters in the middle of the document
r.Find.Execute findtext:="cdef"
'Delete all lowercase letters. Intended to only work on the characters within r
Do While r.Find.Execute(findtext:="[a-z]", MatchWildcards:=True, replacewith:="", Wrap:=wdFindStop)
Loop
End Sub
I want this code to only delete cdef
, but it ends up deleting cdefghij
despite explicitly defining the Wrap
parameter to be wdFindStop
. How can I make the Find function stop at the end of r?