How to switch the highlight color to yellow for text that will be typed in Word 2007?
-
12-12-2019 - |
Question
In a VBA-Macro in MS Word 2007 I want switch the text highlight color to yellow for the text that will be typed next at the cursor position.
If I want to change the color of the currently selected text, I can do that with
Selection.Range.HighlightColorIndex = wdYellow
But that only seems to work if the text already exists and is selected. Can I also set the highlight color to yellow for the text that will be inserted at the cursor position?
(E. g. if I click on the "bold font" button, all future text will be bold, but that does not seem to work with the highlight function, or is there a way to do it?)
Solution
all future text will be bold
No, it won't. By clicking bold
you mark the current point in the text as bold, and if you carry on typing from that point, the text will be bold because it is being typed from a bold place, not because the button is pressed. If you click bold
, move the caret to some other place and type, it won't be bold.
The correct way is to insert the text and apply formatting to its range.
Dim r As Range
Set r = Selection.Range
r.Collapse wdCollapseStart
r.InsertAfter "Text to insert"
r.HighlightColorIndex = wdYellow