VBA word. HighlightColorIndex of a hyperlink retur

2019-07-27 10:14发布

问题:

I'm writing a macro in Word to remove the highlighting from all hyperlinks highlighted with wdGray25. The search for all hyperlinks is done by

For Each oLink In ActiveDocument.Hyperlinks
    If oLink.Range.HighlightColorIndex = wdGray25 Then
    oLink.Range.HighlightColorIndex = wdNoHighlight
    End If
Next oLink

The problem is that highlighting from some hyperlinks highlighted with wdGray25 is not removed. By examining hyperlinks with Alt+f9 it appears that wdGray25 highlighting isn't removed from those hyperlinks whose hyperlink/field (revealed only by Alt+f9) isn't highlighted. These hyperlinks seem normally highlighted when hyperlink/field is hidden. For these hyperlinks Range.HighlightColorIndex returns 9999999.

How can I rewrite the code so that wdGray25 hyperlinks are found even though hyperlink/field isn't highlighted?

回答1:

Find and replace specific formatting

You can search for and replace or remove character formatting in your document. For example, you can search for a specific word or phrase and change the font color, or you can search for specific formatting, such as bold or highlight, and change it.

  1. On the Home tab, in the Editing group, choose Replace. Or press Ctrl+H on your keyboard.

  1. If you don't see the Format button, click More.

  2. To search for text with specific formatting, type the text in the Find what box. To find formatting only, leave the box blank.

  3. Click Format, and then select the formats that you want to find and replace. For example, to find highlighted text, click Format > Highlight; to find bold text, click Format > Font, and then in the Find Font dialog box, select Bold in the Font style list.

  4. Click the Replace with box, click Format, and then select the replacement formats as described in step 4.

Note: If you also want to replace the text, type the replacement text in the Replace with box.

  1. To find and replace each instance of the specified formatting, click Find Next, and then click Replace. To replace all instances of the specified formatting, click Replace All.

(Source)

Also see: How to Remove or turn off hyperlinks



回答2:

you could exploit the knowledge that those "hidden" hyperlinks have 9999999 as HighlightColorIndex property

For Each oLink In ActiveDocument.Hyperlinks
    Select Case oLink.Range.HighlightColorIndex
        Case 9999999, wdGray25
            oLink.Range.HighlightColorIndex = wdNoHighlight
    End Select
Next oLink