Word VBA, Changing the Font Format of Characters in an Equations

In this article I will explain how you can apply formatting to certain characters in a word equation. Below are some of the things you might want to do:

  • Change numbers to italic
  • Change number to bold
  • Change the font color of certain characters
  • Replace certain characters

Example:

In the figure below numeric characters in the equations have been turned to bold:

Example 1, Word, VBA Equations After
Example:

In the example below the Alpha Beta characters have been changed to Red:

Example 2, Color Font Result
All of this can be done using VBA for word.

Formatting characters in an equation is not as simple as apply formatting to the rest of the document. The reason being is that not all characters are recognized by the VBA editor. I have explained more about this topic in the article below:

In order to apply a certain formatting to character in an equation there are several functions that need to be implemented I have explained them in the following sections.


Step 1, Get the Unicode Value of Each Character in the Equation:

As a first step we would need a function that returns an array with the unicode values of the characters in the equation. In the article below I have implemented this function:

The function receives as input the index of the word equation. It returns an array of long values, which are the unicode values associated with the characters in the equation.

The function declaration can be seen below:

'returns an array of long values. Each index is the
'unicode value of one of the characters in the equation
Private Function GetUnicodeValue(ByVal intIndex As Integer) _
As Long()
...
End Function


Step 2, Get the Unicode Value of the Characters to Format:

As mentioned at the start of the article, the VBA editor does not recognize many of the characters used in equations. Therefore we will need to work with their unicode value.

In this step we would need to get the unicode value associated with the characters you wish to format. I have provided a program that does this in the article below:

Example: If we want to change the format of all the numeric characters in the equation we would be looking for characters with a unicode value between [48~57]

Example: If we want to modify operators (+, -, /, *) we would be looking for characters with the unicode values {42, 43, 45, 47}

Example: If we want to modify special characters like ∝, ∇, θ we would be looking for characters with the unicode values {8733, 8711, 952}


Step 3, Get the Index of the Target Characters in the Equation:

The next would be to find the location of the target characters in the word equation. This can easily be done by comparing the values obtained in Step 1 with the target values in Step 2.

The function below receives as input the array from Step 1 and an array with the values from Step 2. It returns an array with the location the characters have been found:

'Receives as input 2 arrays. Returns matching indexes
Private Function GetMatches(ByRef arrUnicodes() As Long, _
ByRef arrTargetValues() As Long) As Long()
Dim i As Integer
Dim j As Integer
Dim K As Integer
'the output array
Dim arrOutput() As Long
ReDim arrOutput(1 To 1)
K = 1
'loops through the first array
For i = LBound(arrUnicodes) To UBound(arrUnicodes)
'loops through the second array
For j = LBound(arrTargetValues) To UBound(arrTargetValues)
'checks if the arrays are matching
If arrTargetValues(j) = arrUnicodes(i) Then
ReDim Preserve arrOutput(1 To K)
arrOutput(K) = i
K = K + 1
End If
Next j
Next i
GetMatches = arrOutput
End Function

Example:

Consider the equation below:

Word, Equation
The unicode value for the character “a” is 97. The code below returns the index of all the occurrences of the character “a”:

Sub Example1()
Dim arrUnicodes() As Long
Dim arrTargets(1 To 1) As Long
Dim arrMatches() As Long
'unicode value for the character "a"
arrTargets(1) = 97
'returns the unicode characters in the equation
arrUnicodes = GetUnicodeValue(1)
'returns the matches
arrMatches = GetMatches(arrUnicodes, arrTargets)
End Sub

The resulting indices are:

Index Character
6 “a”
21 “a”

Step 4, Select the Target Characters in the Word Equation:

The next step would be to implement a function that would select the target characters in the word equation. The function below does this. It receives as input the index of the target character and index of the word equation and selects the character in the equation:

'Recieves 2 input parameters. The index of the equation and the
'index of the character to select
Sub SelectCharacter(ByVal intCharIndex As Integer, _
ByVal intEquIndex As Integer)
'changes the equation to linearize format
OMaths.Item(intEquIndex).Linearize
'select the equation
OMaths.Item(intEquIndex).Range.Select
'moves to the start of the equation
Selection.MoveLeft unit:=wdCharacter, Count:=1
'move to the index of the character
Selection.MoveRight unit:=wdCharacter, Count:=intCharIndex - 1
'selects one character
Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
End Sub

Example:

In the example below the 6th character of the first equation will be selected:

Sub Example2()
Call SelectCharacter(6, 1)
End Sub

Result:

Select 6th character


Example, Change Numeric Values to Bold:

In the example below numeric values in all the equations in the document will be changed to bold:

Sub Example3()
Dim i As Integer
Dim j As Integer
Dim arrTargets(1 To 10) As Long
Dim arrCharacterValues() As Long
Dim arrMatches() As Long

'character unicode values to format
arrTargets(1) = 48
arrTargets(2) = 49
arrTargets(3) = 50
arrTargets(4) = 51
arrTargets(5) = 52
arrTargets(6) = 53
arrTargets(7) = 54
arrTargets(8) = 55
arrTargets(9) = 56
arrTargets(10) = 57
'iterates through the equations in the document
For i = 1 To OMaths.Count
'gets the unicode characters in the equation
arrCharacterValues = GetUnicodeValue(i)
'gets the index of the matches
arrMatches = GetMatches(arrCharacterValues, arrTargets)
'selects and formats
For j = LBound(arrMatches) To UBound(arrMatches)
Call SelectCharacter(arrMatches(j), i)
Selection.Font.Bold = True
Next j
OMaths.Item(i).BuildUp
Next i
End Sub

Consider the following equations:

Example 1, Word, VBA Equations Before

Result:

Example 1, Word, VBA Equations After
The lines below create the arrTargets array. The unicode values associate with the numbers 0~9 are being assigned to the array as explained in Step 2. This array is passed as input to the function in Step 3:

arrTargets(1) = 48
arrTargets(2) = 49
arrTargets(3) = 50
arrTargets(4) = 51
arrTargets(5) = 52
arrTargets(6) = 53
arrTargets(7) = 54
arrTargets(8) = 55
arrTargets(9) = 56
arrTargets(10) = 57

The lines below get the unicode values of the characters in the equation. This was explained in Step 1:

'gets the unicode characters in the equation
arrCharacterValues = GetUnicodeValue(i)

The line below was explained in Step 3:

'gets the index of the matches
arrMatches = GetMatches(arrCharacterValues, arrTargets)

The line below selects the target character in the equation (Step 4):

Call SelectCharacter(arrMatches(j), i)

The line below changes the selected character to bold:

Selection.Font.Bold = True


Example 2, Change Character Color:

In this example the color of the Alpha and Beta characters will be changed to Red:

Sub Example4()
Dim i As Integer
Dim j As Integer
Dim arrTargets(1 To 10) As Long
Dim arrCharacterValues() As Long
Dim arrMatches() As Long

'character unicode values to format
arrTargets(1) = 945
arrTargets(2) = 946
'iterates through the equations in the document
For i = 1 To OMaths.Count
'gets the unicode characters in the equation
arrCharacterValues = GetUnicodeValue(i)
'gets the index of the matches
arrMatches = GetMatches(arrCharacterValues, arrTargets)
'selects and formats
For j = LBound(arrMatches) To UBound(arrMatches)
Call SelectCharacter(arrMatches(j), i)
Selection.Font.ColorIndex = wdRed
Next j
OMaths.Item(i).BuildUp
Next i
End Sub

Result:

Example 2, Color Font Result

You can download the file and code related to this article from the link below:

See also:

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com

Leave a Reply

Your email address will not be published. Required fields are marked *

privacy policy