[英]Copy from Excel Paste in Word
In Excel, I have about 20 sheets with 20 charts in each that I need to copy/paste into Word documents.在 Excel 中,我有大约 20 张纸,每张纸中有 20 个图表,我需要将它们复制/粘贴到 Word 文档中。 One Word doc per Excel sheet.
每个 Excel 表一个 Word 文档。 I found this article with a solution that I modified to accept a ChartObject as a parameter so that I don't have to think about which chart is being copied.
我发现这篇文章有一个解决方案,我修改了它以接受 ChartObject 作为参数,这样我就不必考虑正在复制哪个图表。 I'm getting the following run-time error on the last line where it calls PasteSpecial in the CopyChart2Word() function:
我在 CopyChart2Word() 函数中调用 PasteSpecial 的最后一行出现以下运行时错误:
Which isn't very helpful because it doesn't tell me what is wrong.这不是很有帮助,因为它没有告诉我出了什么问题。 But the chart is pasted into the Word document with half of the data points missing.
但是图表粘贴到 Word 文档中时丢失了一半的数据点。
Code:代码:
Public Function moveCharts()
Dim i As Integer
Dim name As String
Dim ChtObj As ChartObject
Dim dummy As Variant
initGlobals
For i = 0 To UBound(employees)
name = employees(i)
For Each ChtObj In Worksheets(name).ChartObjects
dummy = CopyChart2Word(ChtObj)
Next ChtObj
Next i
End Function
Public Function CopyChart2Word(chartObj As ChartObject)
Dim wd As Object
Dim ObjDoc As Object
Dim FilePath As String
Dim FileName As String
FilePath = "C:\Users\name\Desktop"
'Empty document for now
FileName = "Template.docx"
'check if template document is open in Word, otherwise open it
On Error Resume Next
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
Set ObjDoc = wd.Documents.Open(FilePath & "\" & FileName)
Else
On Error GoTo notOpen
Set ObjDoc = wd.Documents(FileName)
GoTo OpenAlready
notOpen:
Set ObjDoc = wd.Documents.Open(FilePath & "\" & FileName)
End If
OpenAlready:
On Error GoTo 0
'find Bookmark in template doc
wd.Visible = True
'ObjDoc.Bookmarks("Bookmark1").Select
'copy chart from Excel
chartObj.chart.ChartArea.Copy
'insert chart to Bookmark in template doc
'wdPasteMetafilePicture didn't work so I used the numeric value 3
'wdInLine didn't work so I used the numeric value 0
wd.Selection.PasteSpecial Link:=False, _
DataType:=3, _
Placement:=0, _
DisplayAsIcon:=False
End Function
Link to sample chart .链接到示例图表。
I suspect that the error may be caused by different instances of Word being open at the same time.我怀疑该错误可能是由同时打开不同 Word 实例引起的。 In order to eliminate this possibility I recommend to sort out the way you handle Word and the document.
为了消除这种可能性,我建议理清您处理 Word 和文档的方式。 The logic of your code is a little confused.
你的代码逻辑有点混乱。 Please try this instead.
请试试这个。
On Error Resume Next
Set Wd = GetObject(, "Word.Application")
If Err Then Set Wd = CreateObject("Word.Application")
On Error Resume Next
Set ObjDoc = Wd.Documents(Filename)
If Err Then Set ObjDoc = Wd.Documents.Open(FilePath & "\" & Filename)
On Error GoTo 0
I wonder why you need Wd.Visible = True
.我想知道为什么你需要
Wd.Visible = True
。 It should be visible by default.默认情况下它应该是可见的。 Perhaps the Window isn't the ActiveWindow, however.
然而,也许 Window 不是 ActiveWindow。 In fact, Word might not be the active application.
事实上,Word 可能不是活动应用程序。 I don't think that matters to the code.
我认为这对代码无关紧要。
But it should matter greatly to the Selection
object.但这对
Selection
对象应该很重要。 Only the ActiveWindow can have a Selection
.只有 ActiveWindow 可以有一个
Selection
。 Therefore, if you have Excel open and run the code you can't access a Selection
object in Word.因此,如果您打开 Excel 并运行代码,则无法访问 Word 中的
Selection
对象。 And, in reverse, if you have Word open and make a selection and then change to Excel, the Selection
object would be lost.而且,反过来,如果您打开 Word 并进行选择,然后更改为 Excel,则
Selection
对象将丢失。 That might also cause a fatal error.这也可能导致致命错误。 Just follow the rule: "Never
Select
anything in VBA [until the last line of your code].只需遵循以下规则:“永远不要在 VBA 中
Select
任何内容 [直到代码的最后一行]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.