Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用oMath.building方法的Excel/Word方程式?_Vba_Excel_Ms Word_Equation - Fatal编程技术网

Vba 使用oMath.building方法的Excel/Word方程式?

Vba 使用oMath.building方法的Excel/Word方程式?,vba,excel,ms-word,equation,Vba,Excel,Ms Word,Equation,我正在尝试使用VBA在Microsoft Word 2010中自动生成方程式,并将其插入Excel,因为它不支持oMath对象。问题在于oMath.BUILDING方法。它对\sqrt、\times、\delta等内容的解释方式与手工输入时的解释方式不同 例如,在方程中输入代码Celsius=\sqrt(x+y)+sin(5/9倍(华氏-23(\delta)^2))将得到此结果 这很好 但当使用宏VBA或录制宏时,此方法无法正常工作,它会产生如下结果: . 忽略\sqrt、\times、\del

我正在尝试使用VBA在Microsoft Word 2010中自动生成方程式,并将其插入Excel,因为它不支持oMath对象。问题在于oMath.BUILDING方法。它对\sqrt、\times、\delta等内容的解释方式与手工输入时的解释方式不同

例如,在方程中输入代码Celsius=\sqrt(x+y)+sin(5/9倍(华氏-23(\delta)^2))将得到此结果 这很好

但当使用宏VBA或录制宏时,此方法无法正常工作,它会产生如下结果: . 忽略\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

好吧,这样不行。您可以自己进行数学自动更正替换,例如,使用基于以下内容的内容:

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

AFAIC使用“\”来转义特殊字符,如[仍能正常工作。

重复两次,它将解决您的问题

objEq.BuildUp
objEq.BuildUp
objEq.BuildUp
objEq.BuildUp