0% found this document useful (0 votes)
164 views

Word VBA Creating Equations - VBA and VB - Net Tutorials, Education and Programming Services

The document explains how to create equations in Microsoft Word using VBA. It discusses getting the text string associated with an equation, moving the cursor to insert the equation, and provides code examples to create simple and complex equations. Complex equations require breaking the text string into segments and using the ChrW function to convert unicode characters to the proper symbols. Downloading additional files is recommended to access the full code and examples.

Uploaded by

Abbas Amirifard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
164 views

Word VBA Creating Equations - VBA and VB - Net Tutorials, Education and Programming Services

The document explains how to create equations in Microsoft Word using VBA. It discusses getting the text string associated with an equation, moving the cursor to insert the equation, and provides code examples to create simple and complex equations. Complex equations require breaking the text string into segments and using the ChrW function to convert unicode characters to the proper symbols. Downloading additional files is recommended to access the full code and examples.

Uploaded by

Abbas Amirifard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Word VBA Creating Equations - VBA and VB.Net Tutorials, E... https://software-solutions-online.

com/word-vba-creating-equations/

Word VBA Creating Equations


Aug 14, 2015 azurous Equations

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

Contents [hide]

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


Step 2:
Creating Simple Equations :
Creating Complex Equations:

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:

If you copy the equation and paste it in a notepad this is what


you get:

The text in the notepad might seem like nonsense, but the word editor understands this. In order to

1 of 7 6/24/2020, 2:54 PM
Word VBA Creating Equations - VBA and VB.Net Tutorials, E... https://software-solutions-online.com/word-vba-creating-equations/

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:

Step 2: Select the text in the word


document and click on the “Insert
Equation” button on the “Insert” tab

Step 3: Choose professional View:

Result:

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:

Word VBA Using Bookmarks to Move the Cursor


Word VBA, Move Cursor to Start of Document
Word VBA, Move Cursor to End of Document
Word VBA, Move Cursor to End of Line
Word VBA, Move Cursor to Start of Line
Word VBA, Go to Specific Line

2 of 7 6/24/2020, 2:54 PM
Word VBA Creating Equations - VBA and VB.Net Tutorials, E... https://software-solutions-online.com/word-vba-creating-equations/

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

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

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〗 ) “

3 of 7 6/24/2020, 2:54 PM
Word VBA Creating Equations - VBA and VB.Net Tutorials, E... https://software-solutions-online.com/word-vba-creating-equations/

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:

VBA Excel String Processing and Manipulation

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:

VBA Get Characters Unicode Value

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) + " )"

4 of 7 6/24/2020, 2:54 PM
Word VBA Creating Equations - VBA and VB.Net Tutorials, E... https://software-solutions-online.com/word-vba-creating-equations/

Set objOMath = OMaths.Add(objRange).OMaths.Item(1)


objOMath.BuildUp
End Sub

Result:

You can download the file and code


related to this article from the link
below:

Create Word Equation.docm

See also:

Word VBA Equations Linear/Professional Display


Microsoft (MSDN) OMath Object (Word)

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

4 thoughts on “Word VBA Creating Equations”

1.

Kikou says: Apr 28, 2016

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

5 of 7 6/24/2020, 2:54 PM
Word VBA Creating Equations - VBA and VB.Net Tutorials, E... https://software-solutions-online.com/word-vba-creating-equations/

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

Reply

2.

Evan Siegel says: Jan 12, 2019

How about writing a system of equations. Like:


3x + 2y = 14
4x – 7y = 11
?

6 of 7 6/24/2020, 2:54 PM
Word VBA Creating Equations - VBA and VB.Net Tutorials, E... https://software-solutions-online.com/word-vba-creating-equations/

Reply

1.

Bruno Garnier says: Jun 07, 2020

Just add as linear text the following formula :


{█(3x+2y=14@4x-7y=11)┤

Reply

3.

7 of 7 6/24/2020, 2:54 PM

You might also like