Copying a chart from Excel into a Word Template

closet

New Member
Joined
Jun 20, 2016
Messages
2
Hi there,

I have created a chart in Excel and want this to appear at the Bookmark 'CIPChart' in a premade Word template. This is my code. The Word template opens fine but I can't get the chart to copy and paste into it. Where am I going wrong?

Sub macro()

Dim word As Object, templateFile As Object

On Error Resume Next

Set word = GetObject(, "word.application") 'gives error 429 if Word is not open
If Err = 429 Then
Set word = CreateObject("word.application") 'creates a Word application
Err.Clear
End If

Set templateFile = word.documents.Add(Template:="G:\SavInv\2014 Investment Prop\CIP Healthcheck\Dummy 4.docx")

Sheets("Performance Results").Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Select
ActiveChart.ChartArea.Copy
ActiveWindow.Visible = True
word.ActiveDocument.Bookmarks("CIPChart").Range.PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False _
, DisplayAsIcon:=False
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You code doesn't work because, to begin with, you've confused it by naming a variable with the same name as the application you want to use. Furthermore, there is no such Word PasteSpecial argument as 'Format:="Picture (Enhanced Metafile)"'. Your code also unnecessarily activates the worksheet and selects the chart for copying.

Try:
Code:
Sub Demo()
Dim wdApp As Object, wdDoc As Object
Const wdPasteEnhancedMetafile As Long = 9
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Add(Template:="G:\SavInv\2014 Investment Prop\CIP Healthcheck\Dummy 4.docx")
Sheets("Performance Results").ChartObjects("Chart 1").Copy
wdDoc.Bookmarks("CIPChart").Range.PasteSpecial DataType:=wdPasteEnhancedMetafile, Link:=False, DisplayAsIcon:=False
wdApp.Visible = True
End Sub
PS: A docx is NOT a template.

PPS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
 
Upvote 0
Thank you very much for your help Paul. Your code works perfectly and you have given me some great tips to use in future. I'm just learning - can you tell. :)

Kind regards,


You code doesn't work because, to begin with, you've confused it by naming a variable with the same name as the application you want to use. Furthermore, there is no such Word PasteSpecial argument as 'Format:="Picture (Enhanced Metafile)"'. Your code also unnecessarily activates the worksheet and selects the chart for copying.

Try:
Code:
Sub Demo()
Dim wdApp As Object, wdDoc As Object
Const wdPasteEnhancedMetafile As Long = 9
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Add(Template:="G:\SavInv\2014 Investment Prop\CIP Healthcheck\Dummy 4.docx")
Sheets("Performance Results").ChartObjects("Chart 1").Copy
wdDoc.Bookmarks("CIPChart").Range.PasteSpecial DataType:=wdPasteEnhancedMetafile, Link:=False, DisplayAsIcon:=False
wdApp.Visible = True
End Sub
PS: A docx is NOT a template.

PPS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
 
Upvote 0

Forum statistics

Threads
1,215,644
Messages
6,125,992
Members
449,278
Latest member
MOMOBI

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top