Excel VBA - 复制图表并粘贴为增强型图元文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19936141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:23:51  来源:igfitidea点击:

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

excelvbaexcel-vbacharts

提问by Kes Perron

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 Sheet1 中的对象的 Chart1,并将其粘贴为 Book2 Sheet1 中的增强图元文件图片。我从另一个站点获得了此代码,但它不起作用:

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

我收到运行时错误 438:“对象不支持此属性或方法”

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

但是它现在返回运行时错误 1004:“对象 '_Worksheet' 的方法 'PasteSpecial' 失败”。

回答by Tim Williams

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