【问题标题】:Excel/Word Equations using oMath.BuildUp method?使用 oMath.BuildUp 方法的 Excel/Word 方程?
【发布时间】:2013-12-02 19:24:49
【问题描述】:

我正在尝试使用 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))"
    Set objRange = Selection.OMaths.Add(objRange)
    Set objEq = objRange.OMaths(1)
    objEq.BuildUp
    End Sub

【问题讨论】:

    标签: vba excel ms-word equation


    【解决方案1】:

    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 使用“\”来转义 [ 等特殊字符仍然可以正常工作。

    【讨论】:

      【解决方案2】:

      重复两次,它会解决你的问题。

      objEq.BuildUp
      objEq.BuildUp
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多