Home » excel » Excel VBA – Copy a chart and paste as an enhanced metafile

Excel VBA – Copy a chart and paste as an enhanced metafile

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to copy Chart1 that is an object in Book1 Sheet1 and paste it as an enhanced metafile picture in Book2 Sheet1. I got this code from another site, but it doesn’t work:

'Book1 and Book2 previously declared as Workbooks and set
Book1.Sheets(1).ChartObjects(1).CopyPicture
Book2.Sheets(1).Range("B3").Paste

I get Run-time error 438: “Object doesn’t support this property or method”

I would use something like this, but their code references the chart location and I would like to go by the chart number if possible.

UPDATE:

I have revised my code to the following:

'Sheet1 and Sheet2 declared and set as Worksheets
Sheet1.ChartObjects(1).Chart.CopyPicture
Sheet2.PasteSpecial Format:="Picture (Enhanced Metafile)", _
    Link:=False, DisplayAsIcon:=False
With Sheet2.Shapes(Sheet2.Shapes.Count)
    .Top = Sheet2.Range("B3").Top
    .Left = Sheet2.Range("B3").Left
End With

however it now returns Run-time Error 1004: “Method ‘PasteSpecial’ of object ‘_Worksheet’ failed”.

How to&Answers:

For example:

Sheet1.ChartObjects(1).CopyPicture
Sheet1.PasteSpecial _
         Format:="Picture (Enhanced Metafile)", _
         Link:=False, DisplayAsIcon:=False
With Sheet1.Shapes(Sheet1.Shapes.Count)
    .Top = Sheet1.Range("B3").Top
    .Left = Sheet1.Range("B3").Left
End With