Microsoft Word宏:在第一个单元格中添加值

时间:2017-04-22 20:55:32

标签: vba ms-word word-vba

我想构建一个宏来选择文档中的每个表,附加一行并将该行中第一个单元格的值设置为“Text”。

以下是我现在所拥有的:

Sub AddProofRow()
    Dim t As Table
    For Each t In ActiveDocument.Tables
         t.Rows.Add
         NewRow = t.Rows.Last
         NewRow.Cells(t.Rows.Count, 1).Value = "Proof"
    Next
End Sub

但是,当我运行它时会出现错误,我该如何才能完成这项工作?

2 个答案:

答案 0 :(得分:1)

请尝试此代码。

Sub AppendRowWithText()
    ' 24 Apr 2017

    Dim NewRow As Row
    Dim t As Integer

    With ActiveDocument
        For t = 1 To .Tables.Count
            Set NewRow = .Tables(t).Rows.Add
            NewRow.Cells(1).Range.InsertAfter Text:="Text"
        Next t
    End With
End Sub

答案 1 :(得分:0)

默认设置是在表格的末尾添加行。

Sub AddProofRow()
    Dim t As Table
        For Each t In ActiveDocument.Tables
            Set NewRow = t.Rows.Add 'BeforeRow is an optional argument 

            NewRow.Cells(1).Text= "Proof" '.text in word-vba 
        Next
End Sub