vbams-wordword-2010

Cannot save the change of the text color using Word VBA?


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.


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.