1

I am creating a word report through excel VBA. I need math equation to be written but unfortunately, the word document do not autocorrect \pi and \times. Otherwise the equation is printed. Can someone suggest me what is the way forward. Below is the code

Sub AreaSolidBolt(wrdApp As Object, wrdDoc As Object, d As Variant)
Dim objRange As Object
Dim objEq As OMath
Dim aCorrect As OMathAutoCorrectEntry
wrdApp.OMathAutoCorrect.UseOutsideOMath = True

Set objRange = wrdDoc.Range
objRange.Text = "A = \pi/4 \times d^2"
Set objRange = wrdApp.Selection.OMaths.Add(objRange)
    For Each aCorrect In wrdApp.OMathAutoCorrect.Entries
        With objRange
            If InStr(.Text, aCorrect.Name) > 0 Then
                .Text = Replace(.Text, aCorrect.Name, aCorrect.Value)
            End If
        End With
    Next aCorrect
Set objEq = objRange.OMaths(1)
objEq.BuildUp

Set objRange = Nothing
End Sub

I have defined the objects as below in the calling function. Can you please suggest me the way forward.

Set fso = CreateObject("Scripting.FileSystemObject")
Set wrdApp = CreateObject("Word.Application")

If Not fso.FileExists(wrdFileName) Then
    Set wrdDoc = wrdApp.Documents.Add
    wrdApp.Visible = False
    With wrdDoc
        .SaveAs FileName:=wrdFileName
    End With
Else
    Set wrdDoc = wrdApp.Documents.Open(wrdFileName)
    wrdApp.Visible = False
    wrdDoc.Content.InsertAfter vbLf
End If
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Raghu
  • 114
  • 2
  • 10

2 Answers2

1

If the AutoCorrect isn't recognizing something you need to use, then you can write in the corresponding (unicode) character code as part of the equation, using ChrW(). PI is 960, for example.

It's not clear what you consider a "times" character, whether an "x" an "*" or something else. I note that most Math equations don't actually use a character for multiplication, which may be why AutoCorrect isn't picking that up. But you can certainly just type those in?

For example:

 objRange.Text = "A = " & ChrW(960) & "/4 * d^2"
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • I was aware of the use of the Unicode but I want to avoid it since I want to use the replace command later to replace the parameters like d with its value. This way I can write 2 equations once with the parameter and another time with the value to include the example calculation. Again, I do not want to include "*" for the multiplication sign since it is not a standard to write the report. – Raghu Jan 14 '18 at 09:40
  • @Raghu Then it's not clear what you need to do. I can't imagine how using this approach does not make it possible for you to "replace" a variable symbol at a later point. I doubt it's possible to change how the AutoCorrect. works, but if that's what you want to change then you have to ask in an end-user, not a programming forum. – Cindy Meister Jan 14 '18 at 12:29
  • The equation will be passed into the function as a string. Another procedure will process the equation where it will decide to include the number or the symbol. In the above example, another subroutine will decide whether to include d or the number say 10.5. – Raghu Jan 14 '18 at 12:34
  • Then pass it as an array of strings, then you can run the replace looping through the array, and still use ChrW... Also loop to concatenate the string. – Cindy Meister Jan 14 '18 at 13:16
  • Thanks Cindy, that was my idea. replacing the parameters with the highest character first followed by the reduced width parameters. I have lots of such equations with different symbols. It would be very time consuming to use Unicode. If I do not find the satisfying answer, I would go with your suggestion. – Raghu Jan 14 '18 at 22:43
0

I have found the answer myself. The was slightly modified. The error was with the placement of the code line Set objRange = wrdApp.Selection.OMaths.Add(objRange)

Below is the modified code.

Sub AreaSolidBolt(wrdApp As Object, wrdDoc As Object, d As Variant)
Dim objRange As Object
Dim objEq As OMath
Dim aCorrect As OMathAutoCorrectEntry
wrdApp.OMathAutoCorrect.UseOutsideOMath = True

Set objRange = wrdDoc.Range
objRange.Text = "A = \pi/4 \times d^2"
For Each aCorrect In wrdApp.OMathAutoCorrect.Entries
   With objRange
      If InStr(.Text, aCorrect.Name) > 0 Then
          .Text = Replace(.Text, aCorrect.Name, aCorrect.Value)
       End If
   End With
Next aCorrect
Set objRange = wrdApp.Selection.OMaths.Add(objRange)
Set objEq = objRange.OMaths(1)
objEq.BuildUp

Set objRange = Nothing
End Sub
Raghu
  • 114
  • 2
  • 10
  • 1
    If this solved your problem, you should [accept your own answer.](https://stackoverflow.com/help/self-answer) – Mr. T Apr 07 '18 at 08:14