Consulting

Results 1 to 3 of 3

Thread: I can't get the correct color of a hyperlink

  1. #1

    A VBA macro can't get the correct color of a hyperlink in MS Word

    I'm writing a VBA macro that find any font color used except from black (RGB: 0,0,0) and blue.
    If this macro finds a character that isn't black and blue in a paragraph, it shows the text of the paragraph with a message box.

    Option Explicit
    
    Sub test()
    
        Dim oParagraph As Paragraph, oLink As Hyperlink
        Dim char As Range
        Dim bFound As Boolean, i As Integer
        Dim strHex As String, strHex1 As String, strHex2 As String, strHex3 As String
        Dim strHex4 As String, strHex5 As String
        
        bFound = False
        
        For Each oParagraph In ActiveDocument.Paragraphs
        
            For Each char In oParagraph.Range.Characters
                strHex = Hex(char.Font.TextColor.RGB)
                strHex1 = Hex(char.Font.ColorIndex)
                strHex2 = Hex(char.Font.Color)
                strHex3 = Hex(char.Style.Font.Color)
                strHex4 = Hex(char.Style.Font.ColorIndex)
                strHex5 = Hex(char.Style.Font.TextColor.RGB)
    
                If char.Font.Color <> wdColorAutomatic And char.Font.Color <> wdColorBlack _
                   And char.Font.Color <> wdColorBlue Then
    
                    bFound = True
                    GoTo nt_lb
    
                End If
            Next char
    
        Next oParagraph
    
    nt_lb:
        If bFound = True Then
            MsgBox oParagraph.Range.Text
        End If
    
    End Sub
    But, this macro doesn't work for a hyperlink.
    When you execute this macro, this macro shows the pragraph including the hyperlink although the hyperlnk is blue and the remaining text is black in the paragraph.

    My macro checks the following properties for checking the font color:
    * Range.Font.Color
    * Range.Font.TextColor
    * Range.Style.Font.Color
    * Range.Style.Font.TextColor

    But, my macro can't get the blue color from any above property.
    In the hyperlink text "This is a test link.", this macro finds that the first "T" character isn't black and blue.

    Also, I've attached a sample document.

    Please help me.
    Attached Files Attached Files
    Last edited by My Jimmy; 06-09-2022 at 01:07 AM.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,373
    Location
    Cross-posted at:
    A VBA macro can't get the correct color of a hyperlink in MS Word - Stack Overflow
    A VBA macro can't get the correct color of a hyperlink in MS Word (msofficeforums.com)
    A VBA macro can't get the correct color of a hyperlink in MS Word - Microsoft Community
    (and who knows how many others).
    It seems you really don't care how many people waste their time working on essentially the same answer...
    Please read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq...._new_faq_item3. These are rules you agreed to comply with when you signed up here.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    73
    Location
    If you do not want to be banned from help forums, read https://www.excelguru.ca/content.php?184 and the rules here. Follow the recommendations including selecting one primary place for answers and following up in each place you posted.

Tags for this Thread

Posting Permissions

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