Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Macro to Change the Color of all Equations in a Word Document

0.00/5 (No votes)
6 Apr 2019 1  
Change the Font Color of all Equations in a Word Document

Introduction

If you have ever written Mathematical or Scientific Documents in Word, you may have wanted to Change the Color and other Properties of all Equations in your Word Document to make them look uniformly. There is no Function to do this in Word other than manually and you can't really use Styles inside the Equation Editor. What I've come up with is a simple Macro in VBA that let's you change Font Colors either by Name or to a RGB Color. You can easily Modify the Macro to fit your Needs

Using the code

Using the code is really simple, just copy it into the VBA Editor in Word (just press Alt+F11 to open it up), modify the color and Run it.

You can use predefined Colors from Microsoft (https://docs.microsoft.com/en-us/office/vba/api/word.wdcolorindex) or custom RGB colors, you just have to change the corresponding Lines as explained in the Code.

Sub Change_Equation_Color()
'Macro to Change the Font Color of all Equations in a Word Document
Dim Eq As OMath
For Each Eq In ActiveDocument.OMaths
Eq.Range.Select
Selection.Font.ColorIndex = wdDarkBlue 'Choose Color here, e.g. wdBlack
'Selection.Font.TextColor.RGB = RGB(255, 0, 255) 'To use RGB color, uncomment this line and comment the one above
Next
End Sub

If you want to Change other Things in all Equations, just customize the code, for example add

Selection.Font.Italic = False

before the "Next" to disable the Italic option for all Equations.

Other Things that you can do with the "Font" Object in VBA are listed here:

https://docs.microsoft.com/en-us/office/vba/api/word.font

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here