Word VBA, Get the Unicode Value of Each Character in the Equation

In this article I have provided a function that returns the unicode values associated with the characters in an equation.

The function below takes as input the index of an equation and returns an array. The array contains the unicode values associated with the characters in the equations:

'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()
'the output array
Dim arrLongs() As Long
'loops through the characters in the equation
Dim i As Integer
'the number of characters in the equation
Dim intCount As Integer
'the new document
Dim objDoc As Document
Dim flag As Boolean

'initates the array
ReDim arrLongs(1 To 1)
'selects the equations
OMaths.Item(intIndex).Range.Select
Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
'copies the equations
Selection.Copy
'initiates the document
Set objDoc = Documents.Add
objDoc.Activate
'pastes the equation in the new document
Selection.Paste
'remove the equation object
flag = True
While flag = True
    objDoc.OMaths.Item(1).Remove
    If objDoc.OMaths.Count = 0 Then
        flag = False
    End If
Wend
'moves to the start of the equation
Selection.HomeKey unit:=wdStory
'select the entire equation
Selection.EndKey unit:=wdLine, Extend:=wdExtend
'gets the number of characters in the equation
intCount = Len(Selection.Text) - 1
'moves to the start of the equation
Selection.HomeKey unit:=wdStory
'loops through the equations characters
For i = 1 To intCount
    'selects on character
    Selection.MoveRight unit:=wdCharacter, Count:=1, _
    Extend:=wdExtend
    'gets its unicode value
    arrLongs(i) = AscW(Selection.Text)
    'resizes the array
    ReDim Preserve arrLongs(1 To i + 1)
    'moves one step to the right
    Selection.MoveRight unit:=wdCharacter, Count:=1
Next i
objDoc.Close (False)
'returns the array
GetUnicodeValue = arrLongs
End Function

Example: Assume we have the following equation with the index “1”:

Equation
After calling the function we get an array with the following data:

Index Unicode Value Character Index Unicode Value Character
1 102 f 30 12310
2 40 ( 31 110 n
3 120 x 32 960 π
4 41 ) 33 120 x
5 61 = 34 47 /
6 97 a 35 76 L
7 95 _ 36 12311
8 48 0 37 43 +
9 43 + 38 98 b
10 8721 39 95 _
11 95 _ 40 110 n
12 40 ( 41 32
13 110 n 42 32
14 61 = 43 115 s
15 49 1 44 105 i
16 41 ) 45 110 n
17 94 ^ 46 8289
18 8734 47 12310
19 9618 48 110 n
20 40 ( 49 960 π
21 97 a 50 120 x
22 95 _ 51 47 /
23 110 n 52 76 L
24 32 53 12311
25 32 54 32
26 99 c 55 41 )
27 111 o 56 32
28 115 s

Code Explanation:

The code below copies the equation:

'selects the equations
OMaths.Item(intIndex).Range.Select
Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
'copies the equations
Selection.Copy

The code below creates a new word document and pastes the equation on to it:

'initiates the document
Set objDoc = Documents.Add
objDoc.Activate
'pastes the equation in the new document
Selection.Paste

The code below removes the equation object from the new document. What we are left is the plane text associated with the equation:

'remove the equation object
flag = True
While flag = True
    objDoc.OMaths.Item(1).Remove
    If objDoc.OMaths.Count = 0 Then
        flag = False
    End If
Wend

Plane Equation Text
The code below selects the text:

'moves to the start of the equation
Selection.HomeKey unit:=wdStory
'select the entire equation
Selection.EndKey unit:=wdLine, Extend:=wdExtend

Select Equation text
The code below gets the number of characters in the equation:

'gets the number of characters in the equation
intCount = Len(Selection.Text) - 1

The code below moves the cursor to the start of the line:

'moves to the start of the equation
Selection.HomeKey unit:=wdStory

The For I loop, iterates through all the characters in the line:

For i = 1 To intCount
...
Next i

The line below selects the next character:

'selects on character
Selection.MoveRight unit:=wdCharacter, Count:=1, _
Extend:=wdExtend

Select Next Character
The code below stores the unicode value associated with the character in the output array:

'gets its unicode value
arrLongs(i) = AscW(Selection.Text)

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 *