VBA(单词)在表格中写出方程式

时间:2017-06-16 11:21:53

标签: vba ms-word equations

所以我正在开发一个word模板,它需要在文档的特定位置自动编写特定数据。现在我需要在单元格中编写和数学方程式(例如:〖ΔU=α〗_(钢)∙ΔT∙ΔL_vp)我知道我需要用ChrW(###)替换某些字符。但我似乎无法弄清楚如何在单元格中以正确的格式编写公式(代码中的特定位置“我的公式在这里”。注意这只是一个单元格作为一个例子但是有更多的单元格填充在 with activedocument.tables 。这里有人能帮助我吗?

'Selecteren Table
With ActiveDocument.Tables(TableNum)

    'Select cell to write data in
        With .cell(r, 1)
        'data to be written in cell    
        With .Range
                .Text = "My Equation here"
            End With
        End With

end with

只是为了澄清代码的with部分的使用

'选择正确的表格     使用ActiveDocument.Tables(TableNum)

    'add row when a Tee is already inserted
    If insertrow = True Then
        ActiveDocument.Tables(TableNum).cell(r, 1).Select
        Selection.InsertRows (1)
    End If

    'Select cell and write data
        With .cell(r, 1)
            With .Range
                'lettertype updaten voor betreffende cell
                With .Font
                .Bold = True
                End With
                .Text = TxtTstuk.Value & ":"
            End With
        End With

        'Select cell and write data
        With .cell(r, 2)
            With .Range
                .Text = "Type T-stuk:"
            End With
        End With

        'Select cell and write data
        With .cell(r, 3)
            With .Range
                .Text = TxtTType.Value
            End With
        End With

    'add 1 to counter
    r = r + 1

    'Add row
    If insertrow = True Then
        ActiveDocument.Tables(TableNum).cell(r, 1).Select
        Selection.InsertRows (1)
    Else
        ActiveDocument.Tables(TableNum).Rows.Add
    End If

    'Select cell and write data
        With .cell(r, 2)
            With .Range
                .Text = "Diameter doorgaande leiding:"
            End With
        End With

依旧......

1 个答案:

答案 0 :(得分:0)

由于您只使用单个属性,嵌套With的目的是什么?

根据您的需要进行修改。

Sub WriteEq()

    Dim objRange As Range
    Dim objEq As OMath

    With ActiveDocument
        Set objRange = .Tables(1).Cell(1, 1).Range
            objRange.Text = "Celsius = (5/9)(Fahrenheit – 32)"
        Set objRange = .OMaths.Add(objRange)
    End With

    Set objEq = objRange.OMaths(1)
        objEq.ConvertToMathText
        objEq.BuildUp

End Sub