In Excel, I have about 20 sheets with 20 charts in each that I need to copy/paste into Word documents. One Word doc per Excel sheet. 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. I'm getting the following run-time error on the last line where it calls PasteSpecial in the CopyChart2Word() function:
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.
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.