使用 VBA 从 excel 复制粘贴 table 到 Word 中的书签位置

Copy paste a table from excel to a bookmarked location in Word using VBA

我正在尝试从 excel 中的 sheet 复制并 table 并将其粘贴到 word 文档的特定位置,使用 VBA

我试过下面的代码:

Sub Copypastetabe()

Dim strPath As String

'Set path via this excel workbook

strPath = ThisWorkbook.Path & "\" & "Morning Snapshot1" & ".docx"

Dim objWord As Object

Dim docWord As Object

'copy the date table to go to word doc

Sheets("Sheet4").Range("A1:F6").Copy

'define and open word doc

Set objWord = CreateObject("Word.Application")

objWord.Visible = True

Set docWord = objWord.Documents.Open(fileName:=strPath, ReadOnly:=False)

'Select bookmark in word doc

docWord.Bookmarks(BondYields).Select

Selection.Paste

End Sub

我收到错误

Runtime error 5941 "The requested Member of the collection does not exist"

这个word文档中的书签就是这个名字,所以我有点卡

有人能帮忙吗?

'Select bookmark in word doc

docWord.Bookmarks(BondYields).Select

Selection.Paste

应该是:

'Select bookmark in word doc

docWord.Bookmarks(“BondYields”).Select

objWord.Selection.Paste

或者更好:

‘Paste into bookmark in Word doc

docWord.Bookmarks("BondYields").Range.Paste

可能:

docWord.Bookmarks("BondYields").Range.Paste