Word VBA Creating Equations

In this article I will explain how you can create equations using VBA for word.


Step 1, Getting the Text String Associated with the Equation:

The first step for creating an equation in VBA for word is to determine the text string associated with that equation. You might not have noticed but equations in word are a basic text string. For example take a look at the the equation below:

Word Equation
If you copy the equation and paste it in a notepad this is what you get:

Word Equation in NotePad
The text in the notepad might seem like nonsense, but the word editor understands this. In order to create equations in word using VBA, we too will have the create the text string above.

The text above can be converted back to a word equation by following the steps below:

Step 1: Copy the text in the notepad editor and paste it in an empty word document:

Word, Equation Linear
Step 2: Select the text in the word document and click on the “Insert Equation” button on the “Insert” tab

Word Equation, Insert
Step 3: Choose professional View:

Word Equation Professional View
Result:

Word, Equation Professional
As you can see the equation was created from the text string in the notepad file. So basically we need to be able to generate the text string in the notepad file to create the equation in word.

I will explain this in more detail in the following sections.


Step 2:

The next step for creating an equation is to move the cursor to the location you want to insert the equation.For more information about moving the cursor around the document please see the links below:


Creating Simple Equations :

Equations can be made using the code below:

Sub Example1()
Dim objRange As Range
Set objRange = Selection.Range
objRange.Text = "y = x^2+1"
Call OMaths.Add(objRange)
End Sub

Word VBA Insert Equation Restul
Note: If you want to keep reference to the newly created equation you could use the code below. You could then change the display to professional so it looks like a real equation:

Sub Example2()
Dim objRange As Range
Dim objOMath As OMath
Set objRange = Selection.Range
objRange.Text = "y = x^2+1"
Set objOMath = OMaths.Add(objRange).OMaths.Item(1)
objOMath.BuildUp
End Sub

Create equation


Creating Complex Equations:

For more complex equations you can’t simply paste the text string in the VBA editor. For example lets say we want to create the equation explained at the start of the post. The first step would be to replace the text :

  • “y = x^2+1”

with

  • “f(x)=a_0+∑_(n=1)^∞▒(a_n cos⁡〖nπx/L〗+b_n sin⁡〖nπx/L〗 ) “

But this is what will happen if we try to make this replacement:

Sub Example1()
Dim objRange As Range
Set objRange = Selection.Range
objRange.Text = _
"f(x)=a_0+?_(n=1)^8n(a_n cos??npx/L?+b_n sin??npx/L? ) "
Call OMaths.Add(objRange)
End Sub

As you can see a lot of the characters are not recognized by the VBA editor and are replaced by a “?” character. The solution to this is explained in the steps below:

Step 1: Break the text string into different segments, separating the “?” characters:

"f(x)=a_0+" + ? + "_(n=1)^8n(a_n cos"+ ?? + _
"npx/L" + ?+ "+b_n sin' + ?? + "npx/L" +? +" ) "

For more information about concatenating strings please see the link below:

Step 2: Find the unicode value associated with the missing characters. The missing characters are:

I have written a program which returns the unicode value associated with characters. You can download the program at the link below and use it to find the unicode values associated with each of the characters above:

Step 3: Replace the “?” characters with the ChrW() function. The ChrW() function receives as input a unicode value and returns the character associated with it:

Sub Example3()
Dim objRange As Range
Dim objOMath As OMath
Set objRange = Selection.Range
objRange.Text = _
"f(x)=a_0+" + ChrW(8721) + "_(n=1)^8" + ChrW(9618) + _
"(a_n cos" + ChrW(12310) + "npx/L" + ChrW(12311) + _
"+b_n sin" + ChrW(12310) + "npx/L" + ChrW(12311) + " )"
Set objOMath = OMaths.Add(objRange).OMaths.Item(1)
objOMath.BuildUp
End Sub

Result:

Word Equaton Complex
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

 

One thought on “Word VBA Creating Equations”

  1. Kikou says:

    Hello,
    Thank you for the code
    I need to use your code but for Visual Basic 2010 Express
    I tried to turn it, here’s the code :

    Imports System.IO
    Imports Word = Microsoft.Office.Interop.Word
    Public Class Form1
    Dim oWord As New Word.Application
    Dim oDoc As Word.Document
    Dim oPara1 As Word.Paragraph
    Dim objOMath As Word.OMath
    Dim cheminPlusFichier As String
    Private Sub Creer()
    SaveFileDialog1.Filter = “Document Word (*.docx)|*.docx”
    SaveFileDialog1.Title = “Enregistrer sous”
    SaveFileDialog1.FileName = “Notes de calcul”
    If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
    cheminPlusFichier = SaveFileDialog1.FileName
    System.IO.File.WriteAllBytes(cheminPlusFichier, My.Resources.Note_de_calcul)
    End If
    cheminPlusFichier = SaveFileDialog1.FileName
    oDoc = oWord.Documents.Open(SaveFileDialog1.FileName)
    oDoc.Application.DisplayAlerts = False
    oWord.Visible = True
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Creer()
    oPara1 = oDoc.Content.Paragraphs.Add
    oPara1.Range.Text = _
    “f(x)=a_0+” + ChrW(8721) + “_(n=1)^8” + ChrW(9618) + _
    “(a_n cos” + ChrW(12310) + “npx/L” + ChrW(12311) + _
    “+b_n sin” + ChrW(12310) + “npx/L” + ChrW(12311) + ” )”
    objOMath = oWord.OMaths.Add(oPara1).oWord.OMaths.Item(1)
    objOMath.BuildUp()
    End Sub
    End Class

    but its not working
    the equation is displayed as:
    f(x)=a_0+∑_(n=1)^8▒(a_n cos〖npx/L〗+b_n sin〖npx/L〗 )
    and it displays the following error:

    if you can help me that would be cool
    thank you in advance

Leave a Reply

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

Share
Additional Info