Vba 如何将行添加到合并的单词表中?
这是桌子的样子 代码:Vba 如何将行添加到合并的单词表中?,vba,excel,ms-word,Vba,Excel,Ms 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 b
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
运行此操作时,会收到一条错误消息:运行时错误“5941”:请求的集合成员不存在。
注意:我正在运行一个VBA Excel宏,在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
你有没有试过在宏录制器运行的情况下手动查看生成的代码?这正是我想要的!我也不能让你的代码工作。
Set-wdDoc=ActiveDocument
部分把我弄糊涂了。您能否在合并wdFileName
、Set-wdDoc=GetObject(wdFileName)
和使用wdDoc重写部分代码?那太好了!!!我非常感激:)谢谢不要理会那句话:)我用它来测试。您已经在代码中对其进行了初始化Set wdDoc=GetObject(wdFileName)
我可以重新编写,但我希望您理解代码并亲自尝试。如果您遇到问题,请使用您尝试过的代码更新您的问题,然后我们将从那里着手:)我不断收到一个“请求的集合成员不存在”错误,我不确定为什么或如何修复它。编辑:我将用代码更新问题!!对不起,我就是搞不懂这个(我可以看看你的word文件吗?如果可以,你可以上传到一个免费的文件共享网站并在这里共享链接吗
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