在Excel 2007中将多个图表复制为图片会导致应用程序定义的错误
介绍
我似乎无法让ChartObjects.CopyPicture方法在Excel 2007中工作。无论我尝试什么,我都会得到一个错误。
使用这种技术会在CopyPicture线上抛出“应用程序定义或对象定义的错误”。
ActiveSheet.ChartObjects.CopyPicture Appearance:=xlScreen, Format:=xlPicture Sheets("Sheet2").Paste
此方法在PasteSpecial行上抛出“PasteSpecial method of Worksheet class failed”
ActiveSheet.ChartObjects.Copy Sheets("Sheet2").PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False
但是,如果我将图表作为Shape对象使用。
ActiveSheet.Shapes("Chart 6").CopyPicture Appearance:=xlScreen, Format:=xlPicture Sheets("Sheet2").Paste
这也适用
ActiveSheet.Shapes("Chart 6").Copy Sheets("Sheet2").PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False
问题
我的问题是,当我尝试复制多个图表作为一个组失败。
我试图使用Shapes对象的Range属性,但没有可用的CopyPicture方法。 我提出了这个解决方法,但是这也失败了,与我尝试CopyPicture时得到的信息一样。
ActiveSheet.Shapes.Range(Array("Chart 5", "Chart 6")).Select Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture Sheets("Sheet2").Paste
相反,这也不起作用
ActiveSheet.Shapes.Range(Array("Chart 5", "Chart 6")).Select Selection.Copy Sheets("Sheet2").PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False
这个问题已经让我疯狂很久了。 我终于find了一个有效的解决scheme,希望这将有助于未来的其他人。
基本上,解决scheme是将图表分组为单个Shape对象,然后将该graphics的CopyPicture分组,然后在完成时取消组合。
With ActiveSheet.ChartObjects.ShapeRange.Group .CopyPicture Appearance:=xlScreen, Format:=xlPicture .Ungroup End With Sheets("Sheet2").Paste