1
votes

I have been searching off and on, high and low, for many months, to no avail, for VBA code that I can include in my macros to highlight text using the current default highlight color. I have looked through my personal resource books and all up and down the land of Google, including here.

I have a bazillion macros created that highlight text every which way to Sunday, in every possible (basic) color, and they all work great. But sometimes I have a scenario where I want to highlight random text in an ongoing manner, multiple times, in a particular color. Then I'll want to switch colors ... for a while. And switch again ... for a while.

I have keyboard shortcuts (many) for a lot of highlighting options, including the built-in command shortcut to highlight text that I have selected manually. So, for example, I can manually select three words to my left, and use Ctrl+Alt+H to invoke the built-in Highlight command, and it will use the current default color.

But I'm unable to automate that further with a macro. For example, I have used numerous different iterations of macros to highlight text as I'm typing it, or to automatically highlight X number of words to my left, or a line or sentence or paragraph. But with each of those macros, I'm forced to declare a specific color. That means that I then have to have voluminous series of macros to be able to do the same thing with each (basic) color variation. That's fine if I just want to infrequently highlight text in a specific color. But when I know I will be using a specific color for the next, say, 20 or 40 highlights, I'd love to set a default color and then just use a generic highlight command rather than a color-centric command (which is more steps).

1
Any chance you can use sendkeys in Word to simulate Ctrl + Alt + H if that would apply the default? Though the linked article has an answer that recommends WinAPI over sendkeys. I am not that familiar with Word.QHarr
Or store the color in a variable and only update that variable when there is a change. Set the format to that variable? I was also looking at the following for setting a range , having a range.Find and then looping doing stuff......experts-exchange.com/questions/28172730/…QHarr
Alt + T, M, R to record your steps in VBA code. Repeat to stop recording.ACatInLove
Hi QHarr - I've only used SendKeys a handful of times over a long span of years, and each time it was very persnickety and unpredictable. Since I plan to share this macro some colleagues, I'm hesitant to use SendKeys for that reason. But more important, I don't think it would be an option here, for the same reason that I'm in need of a general command to use whatever the current default color is. The shortcut key highlights text after the fact. I need to do it while typing, and there is no shortcut for that. Thanks for the suggestion though! :)HappyNanaMO
QHarr, your second suggestion sounds interesting. I will explore that. Thanks! :)HappyNanaMO

1 Answers

1
votes

After much experimentation, I've finally figured out a way to do it. I'm not sure if this is the most efficient way, but it works!

I have many highlight-centric macros that will use this feature, but here is an example of one of them using this feature. I declared the existing color as a string and then used the string rather than a specific color (which I erroneously thought was a requirement). The following macro allows me to highlight while I'm typing:

    Sub honHighlightOnTypingSelectWord_DEFAULTCOLOR()

        CurColor = Options.DefaultHighlightColorIndex

        Application.ActiveDocument.Application.Options.DefaultHighlightColorIndex = CurColor

            Selection.Words(1).Select
            Selection.Range.HighlightColorIndex = CurColor

    End Sub 

Problem solved!