Question

I am trying to set the text color using Word VBA. However, it seems that Word doesn't take the color change as "change". Take the following VBA code for example, after I run it, Word Undo doesn't contain anything. If I quit the Word directly, Word will not prompt for saving the changes. Could you help to comment? Thank you very much for your opinions!

Public Sub Test()
    ActiveDocument.Range.Font.TextColor = wdColorBlue
End Sub

PS: Word 2010 x86 on Windows 7 SP1 X64.

Was it helpful?

Solution

Instead of using the TextColor property, use the Color property:

ActiveDocument.Range.Font.Color = wdColorBlue

If you do it like this, with Font.Color, Word will register the change and treat it as something to undo. The TextColor property is not officially a configurable property by Word; the docs define it as Read-Only. The fact that you can effectively change the text color via the TextColor property is anomalous, as is the fact that vba's intellisense brings up TextColor and not Color when typing "Font."; nevertheless, it is clear that (at least at one level) Word treats TextColor as read-only, and hence it does not fully register changes to that property.

OTHER TIPS

Word 2013

I tried the code below and no problems. Undo works for each command line. Word as usual prompts to save the changes when closing

Public Sub Test()
   ActiveDocument.Range.Font.TextColor = wdColorBlue
   ActiveDocument.Range.Font.TextColor = wdColorBrightGreen
   ActiveDocument.Range.Font.TextColor = wdColorRed
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top