Hi,
I am trying to loop through a document to find all occurences of a text.
For example, I want to find "link". The sentence in the document is "The links are broken."
Using the code below, I find the first occurence. After that, is set myRange to be from after the occurence to the end. And it does so. Now the myRange becomes "s are broken". When looping, however, Find.Execute finds again "link", going back and expanding the range by itself, even if myRange is "s are broken" and does not contain "link" anymore.
Why instead of looking in myRange, at the second call, Find looks in the whole file again? Of course, stopping on the same occurence as before and looping and looping....
What can I do? What I did wrong?
Thanks a lot. The code is below...
Costin
Set myRange = Doc.Content
ToFind = "link"
WildCards = False
MatchCase = False
MatchWildCards = False
WholeWord = False
Found = False
Do
myRange.Find.ClearFormatting
Found = myRange.Find.Execute(FindText:=ToFind, MatchWildcards:=WildCards, MatchCase:=MatchCase, MatchWholeWord:=WholeWord, Wrap:=wdFindStop, MatchSoundsLike:=False, MatchAllWordForms:=False, Format:=False, Forward:=True)
If Found Then
'Here I fill a list with the positions where I find
myControl.AddItem myRange.Start & " - " & myRange.End & " = " & """" & myRange.Text & """"
End If
myRange.SetRange myRange.End + 1, Doc.Range.End
Loop Until Not Found
I am trying to loop through a document to find all occurences of a text.
For example, I want to find "link". The sentence in the document is "The links are broken."
Using the code below, I find the first occurence. After that, is set myRange to be from after the occurence to the end. And it does so. Now the myRange becomes "s are broken". When looping, however, Find.Execute finds again "link", going back and expanding the range by itself, even if myRange is "s are broken" and does not contain "link" anymore.
Why instead of looking in myRange, at the second call, Find looks in the whole file again? Of course, stopping on the same occurence as before and looping and looping....
What can I do? What I did wrong?
Thanks a lot. The code is below...
Costin
Set myRange = Doc.Content
ToFind = "link"
WildCards = False
MatchCase = False
MatchWildCards = False
WholeWord = False
Found = False
Do
myRange.Find.ClearFormatting
Found = myRange.Find.Execute(FindText:=ToFind, MatchWildcards:=WildCards, MatchCase:=MatchCase, MatchWholeWord:=WholeWord, Wrap:=wdFindStop, MatchSoundsLike:=False, MatchAllWordForms:=False, Format:=False, Forward:=True)
If Found Then
'Here I fill a list with the positions where I find
myControl.AddItem myRange.Start & " - " & myRange.End & " = " & """" & myRange.Text & """"
End If
myRange.SetRange myRange.End + 1, Doc.Range.End
Loop Until Not Found