65.9K
CodeProject is changing. Read more.
Home

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

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (1 vote)

Apr 7, 2019

CPOL
viewsIcon

5351

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