Word VBA Creating Equations - VBA and VB - Net Tutorials, Education and Programming Services
Word VBA Creating Equations - VBA and VB - Net Tutorials, Education and Programming Services
com/word-vba-creating-equations/
In this article I will explain how you can create equations using VBA for word.
Contents [hide]
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:
Result:
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:
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/
Sub Example1()
Dim objRange As Range
Set objRange = Selection.Range
objRange.Text = "y = x^2+1"
Call OMaths.Add(objRange)
End Sub
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
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/
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:
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) + " )"
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/
Result:
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
1.
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/
Reply
2.
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.
Reply
3.
7 of 7 6/24/2020, 2:54 PM