使用 oMath.BuildUp 方法的 Excel/Word 方程? [英] Excel/Word Equations using oMath.BuildUp method?

查看:9
本文介绍了使用 oMath.BuildUp 方法的 Excel/Word 方程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 VBA 在 Microsoft Word 2010 中自动生成方程并将其插入到 Excel 中,因为它不支持 oMath 对象.问题在于 oMath.BuildUp 方法.它不会像手动输入时那样解释 sqrt、 imes、delta 之类的内容.

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, imes, delta in the same way that it is interpreted when entered by hand.

例如输入代码 Celsius = sqrt(x+y) + sin(5/9 imes (Fahrenheit – 23 (delta)^2))http://i43.tinypic.com/10xc7zp.jpg这很好.

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

但是当使用宏 VBA 或录制宏时,此方法无法正常工作,它给出的结果如下:http://i42.tinypic.com/29c5geg.png.像 sqrt, imes, delta 这样的东西被忽略了.为什么?这是我用来生成第二张图片的宏.

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, imes, 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 	imes (Fahrenheit – 23 (delta)^2))"
    Set objRange = Selection.OMaths.Add(objRange)
    Set objEq = objRange.OMaths(1)
    objEq.BuildUp
    End Sub

推荐答案

AFAIK 它只是不能那样工作.您可以进行自己的数学自动更正替换,例如使用基于此的东西:

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

并将您的代码更改为

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

AFAICS 使用"来转义特殊字符,例如 [ 仍然可以正常工作.

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

这篇关于使用 oMath.BuildUp 方法的 Excel/Word 方程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆