Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.
With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company.
Learn more about Allen...
Imagine that you are developing a document that uses text colors to signal special meanings to the reader. For instance, you may use red text to indicate what the user should type. If you are developing a document in which you use color within your text, it can be a real bother to use the menus over and over again to change text colors. In such an instance, it is helpful to have a macro that modifies the color automatically. You can assign the macro to a key combination or a toolbar button so you don't have to use the menus. The following macro changes the color of the currently selected text to red.
Sub MakeRed() If Selection.Type = wdSelectionNormal Or _ Selection.Type = wdSelectionBlock Then Selection.Font.ColorIndex = wdRed Else Beep End If End Sub
If you want to use a different color besides red, change the color assignment made in the macro. (Just change the constant wdRed to the desired color.) VBA allows you to use constants to represent colors. There are seventeen different color numbers available:
|Number||Text Color||Word Constant|
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (253) applies to Microsoft Word 97, 2000, 2002, and 2003.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!