Interop word: How to search in a range for one result per Execute()?
I use Find.Execute() method to search some keyword in the following text in a word document.
a b c d e f 1, a b c d e f 2, a b c d e f 3.
and the search keyword is "a", I want the Execute() return when it finds the first "a". Then in the next Execute() it returns second "a", and in the third Execute() it returns the third "a".
The parameters for Find.Execute() are: MatchWholeWord, (search) Forward, wdFindStop for Wrap.
However after running the Execute() once, it will search whole text and return 3 "a". What should I do to enable the Execute() method return immediately when it finds the first "a", and in next Execute() it returns second "a"?
Thanks for help!
Re: Interop word: How to search in a range for one result per Execute()?
Here is example code
Code:
oDoc = ctl.ActiveDocument
oDoc.ActiveWindow.Selection.WholeStory()
oDoc.ActiveWindow.Selection.Find.ClearFormatting()
oDoc.ActiveWindow.Selection.Find.Replacement.ClearFormatting()
With oDoc.ActiveWindow.Selection.Find
.Text = "a"
.Forward = True
.Wrap = WdFindWrap.wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
oDoc.ActiveWindow.Selection.Find.Execute()
But after running the code, it seems the Find.Execute() will return the last occurence of the word "a". All I need it return the first "a".
Re: Interop word: How to search in a range for one result per Execute()?
personally, i would probably use the vb function instr to do this
however, to answer your question
you need to change the starting position for find range or the selection, (which i would avoid using, much better to work with ranges than the selection, less chance of error) for each execute, to one character after the previous found position