如何将行添加到合并的Word表格?

这是表格的样子。

在这里输入图像说明

码:

Sub WordTableTester() Dim CurrentTable As table Dim wdDoc As Document Dim Rw As Long, col As Long Dim wdFileName wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , "Please choose a file containing requirements to be imported") If wdFileName = False Then Exit Sub '(user cancelled import file browser) Set wdDoc = GetObject(wdFileName) 'open Word file With wdDoc Set CurrentTable = wdDoc.Tables(1) Rw = 9: col = CurrentTable.Columns.Count wdDoc.Range(CurrentTable.Cell(Rw, 1).Range.start, _ CurrentTable.Cell(Rw, col).Range.start).Select wdDoc.Application.Selection.InsertRowsBelow End With End Sub 

当我运行这个,我得到一个错误消息: Run-Time error '5941': The requested member of the collection does not exist.

注意:我正在运行VBA Excelmacros,并将行导入/添加到Word文档中的表中

在MS Word表格中处理合并的行有点棘手。

这是你想要的吗?

 Sub Sample() Dim CurrentTable As Table Dim wdDoc As Document Dim Rw As Long, col As Long Set wdDoc = ActiveDocument '<~~ Created this for testing Set CurrentTable = wdDoc.Tables(1) Rw = 9: col = CurrentTable.Columns.Count wdDoc.Range(CurrentTable.Cell(Rw, 1).Range.Start, _ CurrentTable.Cell(Rw, col).Range.Start).Select wdDoc.Application.Selection.InsertRowsBelow End Sub 

截图 在这里输入图像说明

编辑

你桌子的格式都搞砸了。 表是用很less的行创build的,然后合并/拆分单元格来创build新的行,因此你得到了错误。 另外,既然你是从Excel自动化单词,我会推荐以下方法。

尝试这个

 Sub WordTableTester() Dim oWordApp As Object, oWordDoc As Object, CurrentTable As Object Dim flName As Variant Dim Rw As Long, col As Long flName = Application.GetOpenFilename("Word files (*.docx),*.docx", _ , "Please choose a file containing requirements to be imported") If flName = False Then Exit Sub Set oWordApp = CreateObject("Word.Application") oWordApp.Visible = True Set oWordDoc = oWordApp.Documents.Open(flName) Set CurrentTable = oWordDoc.Tables(1) Rw = 7: col = CurrentTable.Columns.Count oWordDoc.Range(CurrentTable.Cell(Rw, 1).Range.Start, _ CurrentTable.Cell(Rw, col).Range.Start).Select oWordDoc.Application.Selection.InsertRowsBelow End Sub