none
When i highlight part of a Word how to determine the "whole" word ? RRS feed

  • Question

  • Hello, when highlight part of a Word and hence create the Selection.Range or Selection.Text value in VBA, ie lets say i highlight the value of "Range" in the whole word of "RangeValue". And when i'm in the macro is there a way that i can associate the Selection.Range of "Range" with the whole word of which "Range" is a part ie the "whole" word text value of "RangeValue".

    I tried various combintations of Selection.Range.Words.Count and Selection.Range.Characters.Count ect

    Thanks for any clues if possible!
    BobK

    Wednesday, October 5, 2011 12:37 PM

Answers

  • Selection.Range.Words(1)

    will return the first whole word of the selection (or nearest the cursor location if there is nothing selected).

    Selection.Range.Words(Selection.Range.Words.Count)

    will return the entire word of the last word of the selection.


    HTH, Bernie
    Wednesday, October 5, 2011 1:30 PM

All replies

  • Selection.Range.Words(1)

    will return the first whole word of the selection (or nearest the cursor location if there is nothing selected).

    Selection.Range.Words(Selection.Range.Words.Count)

    will return the entire word of the last word of the selection.


    HTH, Bernie
    Wednesday, October 5, 2011 1:30 PM
  • Great thanks alot Bernie! works great have a super week and thanks for the help! ready to roll!

    bk

    Wednesday, October 5, 2011 4:22 PM
  • Ok its working great now Bernie that was the finishing touch as what was happening is if the same subword or snippet encountered another identical snippet it would stop the loop. Ok so when you highlight a word or subword/snippet of a word and select this macro, it will hyperlink thru the document and jump back and stop when it loops back to the original highlighted value. And i thought i would share this as i wouldn't have been able to do it without help from great people like you and on this forum!

    Sub BookmarkSelection
    Dim StrFnd As String, bDirection As Boolean
    Dim fndtotcnt As Integer
    Dim fndloopcnt As Integer
    Dim frstlineno As Integer
    Dim frstpageno As Integer
    Dim exitWhile As String
    Dim SelRangeOrig As Range
    Dim selrangewholeword As String

    Set SelRangeOrig = Selection.Range
    selrangewholeword = Selection.Range.Words(1)

    exitWhile = "n"
    fndtotcnt = 0
    fndloopcnt = 0
    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 fndcnt As Integer
    Dim fndcntP1 As Integer
    fndcnt = 1
    fndcntP1 = 2
    lineno = Selection.Information(wdFirstCharacterLineNumber)
    pageno = Selection.Information(wdActiveEndAdjustedPageNumber)
    If fndcnt = 1 Then
       frstlineno = lineno
       frstpageno = pageno
    End If
    tStrFnd = Trim(StrFnd)
    'tStrFnd = Replace(tStrFnd, "'", "")
    sfndcnt = Str(fndcnt)
    sfndcntP1 = Str(fndcntP1)
    tsfndcnt = Trim(sfndcnt)
    tsfndcntP1 = Trim(sfndcntP1)
    bm = tStrFnd + tsfndcnt
    bm = Replace(bm, " ", "_")
    bm = Replace(bm, "-", "_")
    bm = Trim(bm)
    bmP1 = tStrFnd + tsfndcntP1
    bmP1 = Replace(bmP1, " ", "_")
    bmP1 = Replace(bmP1, "-", "_")
    bmP1 = Trim(bmP1)

    With ActiveDocument.Bookmarks
        .Add bm, Selection.Range
    End With
    With ActiveDocument.Hyperlinks
        .Add Anchor:=Selection.Range, _
         SubAddress:=bmP1, _
         ScreenTip:=bm
    End With

    Dim rangelast As Range
    Set rangelast = Selection.Range
    Dim bmlast As String
    While Selection.Find.Execute
        lineno = Selection.Information(wdFirstCharacterLineNumber)
        pageno = Selection.Information(wdActiveEndAdjustedPageNumber)
        wholeword = Selection.Range.Words(1)
        If fndcnt > 1 And lineno = frstlineno And pageno = frstpageno And selrangewholeword = wholeword Then
           exitWhile = "y"
           MsgBox ("in if bm: " + bm + "bmP1:" + bmP1 + "rl:" + rangelast.Text)
           newbm = tStrFnd + "1"
           newbm = Replace(newbm, " ", "_")
           newbm = Replace(newbm, "-", "_")
           Selection.GoTo What:=wdGoToBookmark, Name:=bm
          'Selection.Range = rangelast
           With ActiveDocument.Hyperlinks
               .Add Anchor:=Selection.Range, _
                SubAddress:=newbm, _
                ScreenTip:=bm
           End With
           Selection.GoTo What:=wdGoToBookmark, Name:=newbm
           Exit Sub
        End If
        fndcnt = fndcnt + 1
        fndcntP1 = fndcnt + 1
        If exitWhile = "n" Then
            si = Len(StrFnd)
               tStrFnd = Trim(StrFnd)
               sfndcnt = Str(fndcnt)
               sfndcntP1 = Str(fndcntP1)
               tsfndcnt = Trim(sfndcnt)
               tsfndcntP1 = Trim(sfndcntP1)
               bm = tStrFnd + tsfndcnt
               bm = Replace(bm, " ", "_")
               bm = Replace(bm, "-", "_")
               bm = Trim(bm)
               bmP1 = tStrFnd + tsfndcntP1
               bmP1 = Replace(bmP1, " ", "_")
               bmP1 = Replace(bmP1, "-", "_")
               bmP1 = Trim(bmP1)
               With ActiveDocument.Bookmarks
                   .Add bm, Selection.Range
               End With
               With ActiveDocument.Hyperlinks
                  .Add Anchor:=Selection.Range, _
                   SubAddress:=bmP1, _
                   ScreenTip:=bm
               End With
        End If
    Wend
    End Sub

     

     

    Wednesday, October 5, 2011 4:33 PM