Search code examples
vbams-wordhighlight

Determine the Highlight color of the first character in a hyperlink


I have a hyperlink (for example www.google.com) that is highlighted, possibly with different colors.
I would like to determine the highlight color of each character in the hyperlink.

I used: r.Hyperlinks(i).Range.Characters(j).HighlightColorIndex
For the very first (j=1) character of the hyperlink (which is w in this case) I get HighlightColorIndex=9999999, regardless of the highlight color. For all the remaining characters (ww.google.com) the code works.

I also tried r.Hyperlinks(i).Range.Characters.First.HighlightColorIndex, but for j=1 it returns 9999999 as well.

How could I get the value of the highlight color of the first character in the hyperlink?


Solution

  • The problem is that the first character of the Range sits "between" when you query it in this manner. One way to see that is

    • Display the Styles pane (in the Home tab, click on the dialog launcher of the "Styles" group)
    • Click in the hyperlink around the third character and look at the style name
    • Use Left Arrow until the insertion point (cursor) is just to the left of the first character in the hyperlink

    When I do that I see the style name switch from "Hyperlink" to "Normal" - IOW Word is looking at what is in effect up to that point, and not at what follows. (I was surprised!). If I then hold Shift and press Right Arrow, I see the Hyperlink style selected, as I'd expect.

    With Hyperlinks, the fact that these are field codes makes working with the first and last characters tricky, because "selecting" them in code picks up the entire field - that's why you're getting 999999 (= undefined - more than one color).

    I could find only one way to work around this: using SendKeys to select the first and last characters as a user does (Shift+Right arrow). I don't like it; it's not going to be very reliable, I fear. The code will not work correctly if you run it from the VBA Editor - it must be run from the document interface where SendKeys should be executed (button in the QAT, for example). The following worked for me:

    Sub HyperlinkHighlight()
        Dim R As word.Range, c As word.Range
        Dim doc As word.Document
        Dim f As word.Field
        Dim i As Long, j As Long
    
        Set doc = ActiveDocument
        Set R = doc.content
    
        For i = 1 To R.Hyperlinks.Count
          R.Hyperlinks(i).Range.Characters(1).Select
          Selection.Collapse wdCollapseStart
          SendKeys "+({Right})", True
          DoEvents
          Debug.Print Selection.Range.HighlightColorIndex
    
          For j = 2 To R.Hyperlinks(1).Range.Characters.Count - 1
            Debug.Print R.Hyperlinks(i).Range.Characters(j).HighlightColorIndex
          Next j
    
          R.Hyperlinks(i).Range.Characters(R.Hyperlinks(i).Range.Characters.Count - 1).Select
          Selection.Collapse wdCollapseEnd
          SendKeys "+({Right})", True
          DoEvents
          Debug.Print Selection.Range.HighlightColorIndex
        Next i
    End Sub