Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 06-09-2022, 12:33 AM
Jimmy Jimmy is offline A VBA macro can't get the correct color of a hyperlink in MS Word Windows 10 A VBA macro can't get the correct color of a hyperlink in MS Word Office 2019
Novice
A VBA macro can't get the correct color of a hyperlink in MS Word
 
Join Date: Jun 2022
Posts: 2
Jimmy is on a distinguished road
Default 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.

Code:
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 getting the correct font color:
  • Range.Font.Color
  • Range.Font.TextColor
  • Range.Style.Font.Color
  • Range.Style.Font.TextColor

But, any of the above properties don't return the correct color (blue).
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
File Type: docm sample.docm (18.2 KB, 5 views)
  #2  
Old 06-09-2022, 03:53 AM
rollis13's Avatar
rollis13 rollis13 is offline A VBA macro can't get the correct color of a hyperlink in MS Word Windows 10 A VBA macro can't get the correct color of a hyperlink in MS Word Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

In your example char.Font.Color of the first character of the hyperlink will return 9999999 and not wdColorBlue. Add the test on that value to this line of code:
Code:
If char.Font.Color <> wdColorAutomatic And char.Font.Color <> wdColorBlack _
   And char.Font.Color <> wdColorBlue Then
  #3  
Old 06-09-2022, 07:07 AM
Jimmy Jimmy is offline A VBA macro can't get the correct color of a hyperlink in MS Word Windows 10 A VBA macro can't get the correct color of a hyperlink in MS Word Office 2019
Novice
A VBA macro can't get the correct color of a hyperlink in MS Word
 
Join Date: Jun 2022
Posts: 2
Jimmy is on a distinguished road
Default

rollis13, thanks for your reply.
I'd like to know why char.Font.Color returns 9999999 other than wdColorBlue.
  #4  
Old 06-09-2022, 07:16 AM
macropod's Avatar
macropod macropod is offline A VBA macro can't get the correct color of a hyperlink in MS Word Windows 10 A VBA macro can't get the correct color of a hyperlink in MS Word Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
I can't get the correct color of a hyperlink
(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...
For cross-posting etiquette, please read: Excelguru Help Site - A message to forum cross posters
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
  #5  
Old 06-09-2022, 08:29 AM
rollis13's Avatar
rollis13 rollis13 is offline A VBA macro can't get the correct color of a hyperlink in MS Word Windows 10 A VBA macro can't get the correct color of a hyperlink in MS Word Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

Probably, since hyperlinks are 'fields' when you test the first character it picks the entire field returning '9999999 (hex 98967F) = undefined Font.Color' since the field has more colors.
Add these 2 lines and deactivate the other 2, run the macro and see the result in the "Immediate Window" Ctrl+G
Code:
'...
strHex2 = Hex(char.Font.Color)
cnt = cnt + 1                         '<- added
Debug.Print cnt & " = " & char.Font.Color & " = " & strHex2 '<- added
strHex3 = Hex(char.Style.Font.Color)
'...
'bFound = True                     '<- deactivated
'GoTo nt_lb                        '<- deactivated
'...
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to change an RGB table cell shading color to another RGB color David Matthews Word VBA 4 05-29-2018 02:45 PM
Correct Bookmark Hyperlink Phil H Word 2 02-10-2016 12:55 PM
A VBA macro can't get the correct color of a hyperlink in MS Word Word 2003 - Macro to color a row in table? cyberpaper Word 2 01-03-2013 02:07 PM
Word macro to email hyperlink pooley343 Word VBA 0 07-20-2011 01:48 AM
A VBA macro can't get the correct color of a hyperlink in MS Word Change Follwed Hyperlink color in Word 2010 Henry Word 1 07-05-2011 04:29 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:19 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft