2017-06-16 105 views
0

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

'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 

等等......

+0

检查:[有没有什么办法来写与VBA中的公式编辑器的公式?(https://social.msdn.microsoft.com/Forums/office/en-US/4d7471bd-30f0 -44e5-8685-79872db03c1f/is-there-any-way-to-write-an-equation-with-the-the-equation-editor-from-vba?forum = exceldev) –

+0

我一直在看那些代码,但看起来不像使它与.cell和.range部分代码相关 –

回答

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 
+0

我对我的问题进行了编辑,以说明为什么我使用'with' –