locked
Find the exact character position in a WORD document or if there's a unique WORD range ID RRS feed

  • Question

  • Hello, i am trying to determine the exact location or ID of a word in a WORD document and i need to do this so i know when i've looped back to the first word found in a "Selection.Find" using the "wdFindContinue" setting. And when i know i've looped back to this first word found in the "Selection.Find" i then GoTo the last bookmark and set its hyperlink to point back to this first word found. 

    So i'm wondering if there is a WORD feature that will exactly ID a particular word or if theres a range coordinate i could use to precisely know when i've looped back to the starting point of the "Selection.Find"? thanks very much! bk  

    With Selection.Find
       .text = StrFnd
       '.ClearFormatting
       '.Replacement.ClearFormatting
       ' Selection.Find.ClearFormatting
       ' Selection.Find.Replacement.ClearFormatting
       '.Replacement.text = vbNullString
       .Forward = True
       .Wrap = wdFindContinue
      '.Wrap = wdFindAsk
       .format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
    End With

    frstlineno = Selection.Range.Information(wdFirstCharacterLineNumber)
    frstpageno = Selection.Range.Information(wdActiveEndAdjustedPageNumber)

    While Selection.Find.Execute
        lineno    = Selection.Range.Information(wdFirstCharacterLineNumber)
        pageno = Selection.Range.Information(wdActiveEndAdjustedPageNumber)
     
        If lineno = frstlineno And pageno = frstpageno Then
           ...reset the last bookmark's hyperlink to point back to this original word

    Wednesday, July 31, 2013 2:12 PM

Answers

  • Yes Jan that's it! i now see based on your example that i should just focus on the Selection.Find.Execute values only and not be concerned about the settings before the Execute loop and the rng.Start looks like its very predictable! great help Jan! thanks again very much!
    • Marked as answer by bobk5444 Thursday, August 1, 2013 3:32 PM
    Wednesday, July 31, 2013 11:24 PM

All replies

  • Bob,

    The code searches all the words strFnd in the document.
    The first time a word is found, its range is saved in a range object (rng).
    When a next time the same range is found (if the start of this range and the start of the first found range is identical) then the code jumps out the do…loop.

    At the end of the code the first found word will be given a bookmark.
    And the last hyperlink-field is searched for and the hyperlink will be changed (overwritten) with the subaddress set to the just mentioned bookmark.
    Although I’m not sure if this is what you wanted to.


    Sub Find_Set_First()
    Dim i As Long
    Dim fld As Field
    Dim rng As Range
    Dim strFnd As String
    strFnd = "Appel"
    With Selection.Find
        .Text = strFnd
        .ClearFormatting
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
     End With

    i = 1
    Set rng = Selection.Range
    Do Until Not Selection.Find.Execute
        If Selection.Range.Start = rng.Start And i > 1 Then
            Exit Do
        End If
        If i = 1 Then
            'the first time the word is found
            Set rng = Selection.Range
        End If
        i = i + 1
    Loop

    If Not rng Is Nothing Then
        For i = 1 To ActiveDocument.Content.Fields.Count
            If ActiveDocument.Content.Fields(i).Type = wdFieldHyperlink Then
                Set fld = ActiveDocument.Content.Fields(i)
            End If
        Next
        If Not fld Is Nothing Then
            ActiveDocument.Bookmarks.Add strFnd, rng
            ActiveDocument.Hyperlinks.Add Anchor:=fld.Code, Address:="", SubAddress:=strFnd, TextToDisplay:="Ga maar naar " & strFnd
        End If
    End If
    End Sub

    Jan

    Wednesday, July 31, 2013 6:17 PM
  • thanks very much Jan! very interesting and i'm definitely going to give this a try right away and i did make some progress with this routine, but it still hiccups on some searches:

    StrFnd = Trim(Selection.text)
    With Selection.Find
       .text = StrFnd
       .ClearFormatting
       .Replacement.ClearFormatting
       .Replacement.text = vbNullString
       .Forward = True
       .Wrap = wdFindContinue
       .format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
    End With

    Dim fparaorig As Paragraph
    Dim fpageno As Integer
    Dim fsr07 As Integer
    Dim fsr08 As Integer
    Dim eparaorig As Paragraph
    Dim epageno As Integer
    Dim esr07 As Integer
    Dim esr08 As Integer

    Set fparaorig = Selection.Range.Paragraphs(1)
    fpageno = Selection.Range.Information(wdActiveEndAdjustedPageNumber)
    fsr07 = Selection.Range.Information(wdHorizontalPositionRelativeToPage)
    fsr08 = Selection.Range.Information(wdHorizontalPositionRelativeToTextBoundary)

    While Selection.Find.Execute
       Set eparaorig = Selection.Range.Paragraphs(1)
       epageno = Selection.Range.Information(wdActiveEndAdjustedPageNumber)
       esr07 = Selection.Range.Information(wdHorizontalPositionRelativeToPage)
       esr08 = Selection.Range.Information(wdHorizontalPositionRelativeToTextBoundary)

       If fparaorig.Range.text = eparaorig.Range.text And fpageno = epageno And fsr07 = esr07 And fsr08 = esr08 Then
          MsgBox (Selection.Range.text)
       End If

    Wend
    End Sub

     

    Wednesday, July 31, 2013 10:51 PM
  • Yes Jan that's it! i now see based on your example that i should just focus on the Selection.Find.Execute values only and not be concerned about the settings before the Execute loop and the rng.Start looks like its very predictable! great help Jan! thanks again very much!
    • Marked as answer by bobk5444 Thursday, August 1, 2013 3:32 PM
    Wednesday, July 31, 2013 11:24 PM
  • Ok Jan many many thanks again! and here is what i ended up with and it works so much  better than before except for some peculiar Table of Contents findings, but overall its alot more predictable than before, so again thanks a million!

    Sub A_FindBookMarkAllGldStd()
    Dim i As Long
    Dim fld As Field
    Dim rng As Range
    Dim strFnd As String
    strFnd = Selection.Range

    With Selection.Find
        .text = strFnd
        .ClearFormatting
        .Forward = True
        .Wrap = wdFindContinue
        .format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
     End With

    i = 1
    Set rng = Selection.Range

    Do Until Not Selection.Find.Execute
       
        If Selection.Range.Start = rng.Start And i > 1 Then
               
           Selection.GoTo WHAT:=wdGoToBookmark, Name:=bm
           pntbm1 = Trim(strFnd) + "_" + "1"
           pntbm1 = Replace(pntbm1, " ", "_")
           si = Len(strFnd)
           With ActiveDocument.Hyperlinks
              .Add Anchor:=Selection.Range.Characters(si - 1), _
               SubAddress:=pntbm1, _
               ScreenTip:=bm
           End With
           Selection.Range.Characters(si - 1).HighlightColorIndex = wdTurquoise
           
           Exit Do
        End If
       
        If i = 1 Then
            Set rng = Selection.Range
        End If
      
        If Selection.Range.Hyperlinks.Count > 0 Then
           srha = Selection.Range.Hyperlinks(1).Address
           srhsa = Selection.Range.Hyperlinks(1).SubAddress
           srht = Selection.Range.Hyperlinks(1).Type
           Selection.Range.Hyperlinks(1).Delete
        End If
           
        bm = Trim(strFnd) + "_" + CStr(i)
        bm = Replace(bm, " ", "_")
        ip1 = i + 1
        bmP1 = Trim(strFnd) + "_" + CStr(ip1)
        bmP1 = Replace(bmP1, " ", "_")
       
        With ActiveDocument.Bookmarks
            .Add bm, Selection.Range
        End With
         
        si = Len(strFnd)
        With ActiveDocument.Hyperlinks
            .Add Anchor:=Selection.Range.Characters(si - 1), _
             SubAddress:=bmP1, _
             ScreenTip:=bm
        End With
        Selection.Range.Characters(si - 1).HighlightColorIndex = wdRed
       
        If srha <> "" Then
           Selection.Range.InsertAfter (">")
           With ActiveDocument.Hyperlinks
               .Add Anchor:=Selection.Range.Next, _
                    Address:=srha, _
                    ScreenTip:=bm
           End With
           srha = ""
         End If
              
         If srhsa <> "" Then
            Selection.Range.InsertAfter (">")
            With ActiveDocument.Hyperlinks
                .Add Anchor:=Selection.Range.Next, _
                 SubAddress:=srhsa, _
                 ScreenTip:=bm
            End With
            srhsa = ""
         End If
          
        i = i + 1

    Loop

    End Sub

    Wednesday, August 7, 2013 1:58 PM