从 Excel 复制粘贴到 Word 中
Copy from Excel Paste in Word
在 Excel 中,我有大约 20 个 sheet,每个有 20 个图表,我需要 copy/paste 到 Word 文档中。每个 Excel sheet 一个 Word 文档。我发现 this article 的解决方案经过修改以接受 ChartObject 作为参数,这样我就不必考虑要复制哪个图表。我在 CopyChart2Word() 函数中调用 PasteSpecial 的最后一行收到以下 运行 时间错误:
这不是很有帮助,因为它没有告诉我哪里出了问题。但是图表粘贴到 Word 文档中时,一半的数据点丢失了。
代码:
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 到 sample chart.
我怀疑这个错误可能是因为同时打开了不同的Word实例。为了排除这种可能性,我建议整理一下你处理Word和文档的方式。您的代码逻辑有点混乱。请试试这个。
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
我想知道你为什么需要 Wd.Visible = True
。它应该默认可见。然而,也许 Window 不是 ActiveWindow。事实上,Word 可能不是活动的应用程序。我认为这对代码无关紧要。
但这对 Selection
对象应该很重要。只有 ActiveWindow 可以有一个 Selection
。因此,如果您 Excel 打开并且 运行 代码,您将无法访问 Word 中的 Selection
对象。反过来,如果您打开 Word 并进行选择,然后更改为 Excel,则 Selection
对象将会丢失。这也可能导致致命错误。只需遵循以下规则:“永远不要 Select
VBA 中的任何内容 [直到代码的最后一行]。
在 Excel 中,我有大约 20 个 sheet,每个有 20 个图表,我需要 copy/paste 到 Word 文档中。每个 Excel sheet 一个 Word 文档。我发现 this article 的解决方案经过修改以接受 ChartObject 作为参数,这样我就不必考虑要复制哪个图表。我在 CopyChart2Word() 函数中调用 PasteSpecial 的最后一行收到以下 运行 时间错误:
这不是很有帮助,因为它没有告诉我哪里出了问题。但是图表粘贴到 Word 文档中时,一半的数据点丢失了。
代码:
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 到 sample chart.
我怀疑这个错误可能是因为同时打开了不同的Word实例。为了排除这种可能性,我建议整理一下你处理Word和文档的方式。您的代码逻辑有点混乱。请试试这个。
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
我想知道你为什么需要 Wd.Visible = True
。它应该默认可见。然而,也许 Window 不是 ActiveWindow。事实上,Word 可能不是活动的应用程序。我认为这对代码无关紧要。
但这对 Selection
对象应该很重要。只有 ActiveWindow 可以有一个 Selection
。因此,如果您 Excel 打开并且 运行 代码,您将无法访问 Word 中的 Selection
对象。反过来,如果您打开 Word 并进行选择,然后更改为 Excel,则 Selection
对象将会丢失。这也可能导致致命错误。只需遵循以下规则:“永远不要 Select
VBA 中的任何内容 [直到代码的最后一行]。