Excel VBA 450 Tables.Add

我目前正在Excel中学习VBA来从电子表格数据生成Word文档。

我得到错误450:错误的参数数量或无效的属性分配。

我不知道什么是错的。 谁能帮我?

除此之外,我该怎么做在表格中的单元格内插入string?

谢谢。

'Create document Sub main() Dim objWord As Object Dim objDoc As Object Set objWord = CreateObject("Word.Application") objWord.Visible = True Set objDoc = objWord.Documents.Add() Dim i As Integer Dim strValue As String For i = 1 To 5 objDoc.Activate 'define string strID = Cells(i, 1) strSTR = Cells(i, 2) strKK = Cells(i, 3) strDEF = Cells(i, 4) strEX = Cells(i, 5) 'write the value to the document objWord.Selection.TypeText Text:=strID + ". " + strSTR + " " + strKK + " " + strDEF 'move to the next line objWord.Selection.TypeParagraph objWord.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:=3 Next i End Sub 

这个怎么样:

 objDoc.Tables.Add Range:=objWord.Selection.Range, NumRows:=4, NumColumns:=3 

这是使用objDoc而不是objWord,并使用objWord.Selection而不是仅仅select