当我将图表从Excel(“报表”表)复制到WORD文件(“报表template.docx”)时,为什么VBA会清除WORD文件的先前内容?我怀疑问题是'wddoc.Range.Paste',但我不知道如何更改它以避免问题。
Sub ActivateWordTransferData()
Dim wdapp As Object, wddoc As Object
Dim strdocname As String
Set wdapp = GetObject(, "Word.Application")
wdapp.Visible = True
strdocname = "C:\users\ian\Documents\Dropbox\Report template.docx"
Set wddoc = wdapp.documents(strdocname)
Worksheets("Report").Shapes("Chart 2").Copy
wdapp.Activate
wddoc.bookmarks("bkmark4").Select
wddoc.Range.Paste
wddoc.Save
Set wddoc = Nothing
Set wdapp = Nothing
Application.CutCopyMode = False
End Sub
答案 0 :(得分:1)
我不确定为什么Word文档的内容被覆盖。
然而,删除.Select操作并粘贴到书签的范围似乎有效。
删除以下行:
wddoc.bookmarks("bkmark4").Select
wddoc.Range.Paste
并替换为此行:
wddoc.bookmarks("bkmark4").Range.Paste