Share via

How can I highlight multiple sections using VBA?

Anonymous
2015-08-06T14:54:00+00:00

Split from this thread.

Hey,

I'm working on MSWord Mac 2011.  I've created macros, but I cannot highlight multiple selections to get them highlighted all at once (it only highlights the last selection).  I created one that used a background color instead of a highlight (I wanted red but not the RED RED of the highlight color), and that one does multiple selections at once and the same with creating a blue italics.  I tried cutting and pasting from the one that works to the highlight, but got errors.  Below is the Macro code.  1 and 2 work on multiple selections (using the Open Apple/Command button), 3 will only highlight the last range selected.

Any thoughts?

Sub Macro1()

'

' Macro1 Macro

' Pale Red Highlight

'

    With Selection.Font.Shading

        .Texture = wdTextureNone

        .ForegroundPatternColor = wdColorAutomatic

        .BackgroundPatternColor = 9868799

    End With

    With Options

        .DefaultBorderLineStyle = wdLineStyleSingle

        .DefaultBorderLineWidth = wdLineWidth050pt

        .DefaultBorderColor = wdColorAutomatic

    End With

End Sub

Sub Macro2()

'

' Macro2 Macro

'

'

    Selection.Font.Italic = wdToggle

    Selection.Font.Color = wdColorBlue

End Sub

Sub Macro3()

'

' Macro3 Macro

' Yellow Highlight

'

    Selection.Range.HighlightColorIndex = wdYellow

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2015-08-15T11:16:17+00:00

The following VBA will add a highlight color to all selected text.

Sub ApplyHighlightColor()

    'since the find/replace only applies a single highlight color you

    'must first set the default highlight color to apply

    Options.DefaultHighlightColorIndex = wdBrightGreen

    'the following assumes the selected text does not

    'already contain a highlight

    With Selection.Find

        .ClearFormatting

        .Highlight = False

        .Replacement.ClearFormatting

        .Replacement.Highlight = True

        .Execute Replace:=wdReplaceAll

    End With

End Sub

The following link provides the enumeration values for highlights. There are not RGB or color code values for highlight colors in the object model.

https://msdn.microsoft.com/en-us/library/Bb237561(v=office.12).aspx

Hope this helps

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2015-08-15T04:10:21+00:00

I'm sure color codes can be found on line in MS's VBA pages. But you can also open the VBA Editor, then

  • Select View > Object Browser (see 1 below)
  • Type "wdcolor" in the search field (2)
  • See the list of colors (clicking on each displays the value at the bottom)

There is a correspondence between the contant names (3) and the color's name (5) as shown in the Shading Tab (4).

Highlight was never fully integrated to Word. To search for highlighted text you use a separate "Highlight" choice in the Advanced Find's dropdown menu and you can't specify a color! It lets you cycle between "Highlight," "No highlight" and "Don't care" each time you select it. Highlight's best value is being a handy tool, but to VBA it is not a font attribute, as you've already seen.

To simulate multiple selections you can apply a rare font attribute to various portions of the text, then do a global replacement. For instance, apply white shading, white underline or marching red ants (which still works despite now being invisible), then replace by what you want.

Was this answer helpful?

0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-08-17T13:31:50+00:00

    Thanks for this information.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-08-14T22:13:25+00:00

    John,

    Thanks for your reply.  

    Taking a different tack, then...

    Do you know where I could find the color codes for the highlight colors?  The red that I used for a background color does allow for multiple selections, so while I have most of the document done with highlight colors, I could use background colors for the remainder.  AND... I've not done much by way of automator, but is there a coding I could use to go through the entire document and change highlights to a specific background color?

    Was this answer helpful?

    0 comments No comments
  3. John Korchok 232.8K Reputation points Volunteer Moderator
    2015-08-06T19:38:31+00:00

    Unfortunately, while the user interface support multiple selections, VBA doesn't.

    Was this answer helpful?

    0 comments No comments