我正在尝试使用 VBA 在 Microsoft Word 2010 中自动生成方程式并将其插入 Excel 中,因为它不支持 oMath 对象。问题出在 oMath.BuildUp 方法中。它不会像手动输入时那样解释 \sqrt、\times、\delta 等内容。
例如输入代码摄氏度 = \sqrt(x+y) + sin(5/9 \times (华氏度 – 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))"
Set objRange = Selection.OMaths.Add(objRange)
Set objEq = objRange.OMaths(1)
objEq.BuildUp
End Sub
AFAIK 它只是不那样工作。您可以进行自己的数学自动更正替换,例如使用基于此的东西:
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 \times (Fahrenheit – 23 (\delta)^2))")
AFAICS 使用“\”转义特殊字符(例如 [)仍然可以正常工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)