Consulting

Results 1 to 7 of 7

Thread: Solved: Find Nth Instance of a Word in Word

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Solved: Find Nth Instance of a Word in Word

    I want to find the 10th instance of the word "fox" in the attached document. Can someone make my code loop and then highlight the 10th one and stop searching? The tenth one is clearly underlined and bolded in my sample.

    Thanks a bunch!
    ~Anne Troy

  2. #2
    VBAX Expert brettdj's Avatar
    Joined
    May 2004
    Location
    Melbourne
    Posts
    649
    Location
    no loop needed

    [vba]
    Sub DreamboatFox()
    Dim Regex, RegM
    Dim Mystr As String
    Mystr = "fox"
    Set Regex = CreateObject("vbscript.regexp")
    With Regex
    .Pattern = Mystr
    .Global = True
    End With
    Set RegM = Regex.Execute(ActiveDocument.Content)
    If RegM.Count > 9 Then
    ActiveDocument.Content.Characters(RegM(9).firstindex + 1).Select
    With Selection
    .MoveRight Unit:=wdCharacter, Count:=Len(Mystr) - 1, Extend:=wdExtend
    .Font.Bold = True
    .Font.Underline = True
    End With
    End If
    End Sub
    [/vba]

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    GREAT!! Now go post it "over there". LOL!!

    http://www.experts-exchange.com/Appl..._21382123.html
    ~Anne Troy

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Better yet, post it to the Kb and post a link "Over There".

  5. #5
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    There's already a link over there to this thread. LOL!!
    But that was my whole purpose. Sounded like a nice KB entry to me.
    ~Anne Troy

  6. #6
    VBAX Expert brettdj's Avatar
    Joined
    May 2004
    Location
    Melbourne
    Posts
    649
    Location
    LOL

  7. #7
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by brettdj
    no loop needed
    Why?? (I love looping)

    No really, regular expressions is the way to go.
    So brilliant coding!

    For all the other "Loop lovers" on off the many other possibillities but now with a loop (Just "Yoinking" you...learned it today..)[VBA]
    Sub GetTheFox()
    MarkIt "fox"
    End Sub
    Sub MarkIt(sFind As String)
    Dim oWord As Word.Range
    Dim iCnt As Integer
    For Each oWord In ActiveDocument.Range.Words
    If Trim(oWord.Text) = sFind Then
    iCnt = iCnt + 1
    If iCnt = 10 Then
    With ActiveDocument.Range(Start:=oWord.Start, End:=oWord.End - 1)
    .Bold = True
    .Underline = True
    End With
    Exit For
    End If
    End If
    Next oWord
    End Sub
    [/VBA]
    See Yah!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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