Excel/Word Equations using oMath.BuildUp method?

流过昼夜 提交于 2019-11-30 16:14:36

问题


I am trying to automate process of generating Equations in Microsoft Word 2010 using VBA and inserting those into Excel since it doesnt support oMath object. Problem lies in oMath.BuildUp method. It doesnt interpret stuff like \sqrt, \times, \delta in the same way that it is interpreted when entered by hand.

For example entering code Celsius = \sqrt(x+y) + sin(5/9 \times (Fahrenheit – 23 (\delta)^2)) into Equation will give this result http://i43.tinypic.com/10xc7zp.jpg which is fine.

But when using macro VBA or recording macro this Method is not working as it should be and it gives result like this: http://i42.tinypic.com/29c5geg.png. Stuff like \sqrt, \times, \delta is ignored. Why? Here is macro that I used to generate second picture.

    Sub genEQ()
    Dim objRange As Range
    Dim objEq As OMath 
    Set objRange = Selection.Range
    objRange.Text = "Celsius = \sqrt(x+y) + sin(5/9 \times (Fahrenheit – 23 (\delta)^2))"
    Set objRange = Selection.OMaths.Add(objRange)
    Set objEq = objRange.OMaths(1)
    objEq.BuildUp
    End Sub

回答1:


AFAIK it just doesn't work that way. You could do your own math autocorrect substitution, e.g. using something based on this:

Function mathSubstitute(s As String) As String
Const bslash As String = "\"
Dim a() As String
Dim sout As String
Dim i As Integer
Dim j As Integer
Dim sac As String
sout = ""
If s <> "" Then
  a = Split(s, bslash)
  sout = a(LBound(a))
  For i = LBound(a) + 1 To UBound(a)
    Debug.Print a(i)
    For j = 1 To Len(a(i))
      On Error Resume Next
      sac = Application.OMathAutoCorrect.Entries(bslash & Left(a(i), j)).Value
      If Err.Number = 0 Then
        sout = sout & sac & Mid(a(i), j + 1)
        Exit For
      Else
        sac = ""
        Err.Clear
      End If
    Next
    If sac = "" Then sout = sout & bslash & a(i)
    Debug.Print sout
  Next
End If
On Error GoTo 0
mathSubstitute = sout
End Function

and change your code to

objRange.Text = mathSubstitute("Celsius = \sqrt(x+y) + sin(5/9 \times (Fahrenheit – 23 (\delta)^2))")

AFAICS the use of "\" to escape special characters such as [ still works correctly.




回答2:


Repeat it twice, it will solve your problem.

objEq.BuildUp
objEq.BuildUp


来源:https://stackoverflow.com/questions/20068212/excel-word-equations-using-omath-buildup-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!