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

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

问题描述

我试图使用VBA自动化在Microsoft Word 2010中生成方程式的过程,并将它们插入到Excel中,因为它不支持oMath对象。问题在于oMath.BuildUp方法。它不会解释像\sqrt,\times,\delta这样的东西,就像在手动输入时解释一样。



例如输入代码 Celsius = \sqrt(x + y)+ sin(5/9 \times(Fahrenheit - 23(\delta)^ 2))转换为方程式将给出此结果
http://i43.tinypic.com/10xc7zp.jpg
这是罚款。



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

  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))
设置objRange = Selection.OMaths.Add(objRange)
设置objEq = objRange.OMaths(1)
objEq.BuildUp
End Sub


解决方案

AFAIK它不会这样工作。你可以做自己的数学自动校正替换,例如使用基于此的东西:

 函数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 =
如果s<> 然后
a = Split(s,bslash)
sout = a(LBound(a))
对于i = LBound(a)+ 1 To UBound(a)
调试。打印a(i)
对于j = 1到Len(a(i))
On Error Resume Next
sac = Application.OMathAutoCorrect.Entries(bslash& Left(a(i ),j))。值
如果Err.Number = 0然后
sout = sout&囊&中(a(i),j + 1)
退出
Else
sac =
Err.Clear
结束如果
下一个
如果sac =然后sout = sout& bslash& a(i)
Debug.Print sout
下一个
结束如果
错误GoTo 0
mathSubstitute = sout
结束函数

并将您的代码更改为

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

AFAICS使用\来转义特殊字符,如[仍然正常工作。


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

解决方案

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.

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

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