vba 在 Excel 2007 中将多个图表复制为图片会出现应用程序定义的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/687353/
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-11 10:19:20  来源:igfitidea点击:

Copying Multiple Charts as a Picture in Excel 2007 gives Application-defined error

excelvba

提问by bendewey

Introduction

介绍

I can't seem to get the the ChartObjects.CopyPicture method to work in Excel 2007. No matter what I try I get an error.

我似乎无法让 ChartObjects.CopyPicture 方法在 Excel 2007 中工作。无论我尝试什么,都会出现错误。

Using this technique throws an 'Application-defined or object-defined error' on the CopyPicture line.

使用此技术会在 CopyPicture 行上引发“应用程序定义或对象定义错误”。

ActiveSheet.ChartObjects.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Sheets("Sheet2").Paste

This method throws as 'PasteSpecial method of Worksheet class failed' on the PasteSpecial line

此方法在 PasteSpecial 行上抛出“Worksheet 类的 PasteSpecial 方法失败”

ActiveSheet.ChartObjects.Copy    
Sheets("Sheet2").PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False

However, If I use the chart as a Shape object it works.

但是,如果我将图表用作 Shape 对象,则它可以工作。

ActiveSheet.Shapes("Chart 6").CopyPicture Appearance:=xlScreen, Format:=xlPicture
Sheets("Sheet2").Paste

This also works

这也有效

ActiveSheet.Shapes("Chart 6").Copy
Sheets("Sheet2").PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False

Problem

问题

My problem is that when I try to copy multiple charts as a group this fails.

我的问题是,当我尝试将多个图表作为一个组复制时,这会失败。

I tried to use the Range property of the Shapes object, but there is no CopyPicture method available. I came up with this work-around, but this also failed, with the same messages that I got when trying to CopyPicture.

我尝试使用 Shapes 对象的 Range 属性,但没有可用的 CopyPicture 方法。我想出了这个解决方法,但这也失败了,与我在尝试 CopyPicture 时收到的消息相同。

ActiveSheet.Shapes.Range(Array("Chart 5", "Chart 6")).Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Sheets("Sheet2").Paste

And conversely this didn't work either

相反,这也不起作用

ActiveSheet.Shapes.Range(Array("Chart 5", "Chart 6")).Select
Selection.Copy
Sheets("Sheet2").PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False

采纳答案by bendewey

This issue has been driving me crazy for a long time. I finally found a solution that works, hopefully this will help someone else in the future.

这个问题已经让我发疯了很长时间。我终于找到了一个有效的解决方案,希望这会在未来对其他人有所帮助。

Basically the solution was to Group the Charts as a single Shape object, then CopyPicture on that shape, then Ungroup when your finished.

基本上解决方案是将图表分组为单个形状对象,然后在该形状上复制图片,然后在完成后取消组合。

With ActiveSheet.ChartObjects.ShapeRange.Group
    .CopyPicture Appearance:=xlScreen, Format:=xlPicture
    .Ungroup
End With

Sheets("Sheet2").Paste