VBA (word) 在 table 中写一个等式
VBA (word) Writing a equation in a table
所以我正在开发一个word模板,需要在文档的特定位置自动写入特定数据。现在我需要在单元格中写数学方程式(例如:〖∆U=α〗_(steel )∙ ∆T ∙ ∆L_vp) 我知道我需要用 ChrW(## #)。但我似乎无法弄清楚如何在单元格中以正确的格式编写公式(具体位置在下面的代码中 "my equation here"。请注意,这只是一个单元格作为示例,但下面还有更多单元格和 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
部分代码的使用
'Select对table
使用 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
等等...
既然您只使用了一个 属性,那么嵌套 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
所以我正在开发一个word模板,需要在文档的特定位置自动写入特定数据。现在我需要在单元格中写数学方程式(例如:〖∆U=α〗_(steel )∙ ∆T ∙ ∆L_vp) 我知道我需要用 ChrW(## #)。但我似乎无法弄清楚如何在单元格中以正确的格式编写公式(具体位置在下面的代码中 "my equation here"。请注意,这只是一个单元格作为示例,但下面还有更多单元格和 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
部分代码的使用
'Select对table 使用 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
等等...
既然您只使用了一个 属性,那么嵌套 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