我需要将表格复制到现有的Word文档中 我需要将数据粘贴到Word文档中的特定位置,例如在书签之后
我有一个代码可以复制和粘贴,但不能粘贴到现有文档中。 我尝试扩展/更改代码,但无法弄清楚如何粘贴到目标。
Sub PasteIntoWord()
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim objWord
Dim ExcRng As Range
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
Set WrdDoc = WrdApp.Documents.Add
Set ExcRng = ActiveSheet.Range("testdata")
ExcRng.copy
WrdDoc.Paragraphs(1).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=True, RTF:=False
Application.CutCopyMode = False
End Sub
这有效,并粘贴到新文档中。 但我想将数据粘贴到此文档中:wordApp.Documents.Open“ c:\ users \ peter \ documents \ Data skal ind her.docm”
我需要在这里放它:
Here is text part 1
And I would like to have my “testdata” pasted here:
Xxx
This is bookmark ”xxx”
最好的问候 彼得 pg@pb.dk
答案 0 :(得分:0)
我找到了这个Word MVP文档,该文档提供了用于更新书签中文本的功能。我已将其添加到您的示例代码中:
Sub PasteIntoWord()
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim objWord
Dim ExcRng As Range
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
Set WrdDoc = wordApp.Documents.Open "c:\users\peter\documents\Data skal ind her.docm"
Set ExcRng = ActiveSheet.Range("testdata")
UpdateBookmark "xxx", ExcRng
End Sub
Sub UpdateBookmark(BookmarkToUpdate As String, PasteRange As Variant)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange = PasteRange
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
来源:https://wordmvp.com/FAQs/MacrosVBA/InsertingTextAtBookmark.htm