简体   繁体   中英

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

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM