Results 1 to 3 of 3

Thread: Interop word: How to search in a range for one result per Execute()?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    74

    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!
    Last edited by tommeal; Apr 28th, 2011 at 08:04 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    74

    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".

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width