简体   繁体   English

使用 VBA 将表格从 Excel 复制粘贴到 Word 中的书签位置

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

I am trying to Copy and table from a sheet in excel and paste it into a word document, in a specific place, using VBA我正在尝试使用 VBA 从 Excel 中的工作表复制和表格并将其粘贴到特定位置的 Word 文档中

I have tried the code below:我试过下面的代码:

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

I get the error我收到错误

Runtime error 5941 "The requested Member of the collection does not exist"运行时错误 5941“请求的集合成员不存在”

The bookmark exists in this word document under this name, so i'm a bit stuck书签以这个名字存在于这个word文档中,所以我有点卡住了

Please can anyone help?请问有人可以帮忙吗?

'Select bookmark in word doc

docWord.Bookmarks(BondYields).Select

Selection.Paste

Should be:应该:

'Select bookmark in word doc

docWord.Bookmarks(“BondYields”).Select

objWord.Selection.Paste

Or better still:或者更好:

‘Paste into bookmark in Word doc

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

大概:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM