vba 如何将图表从 Excel 复制到 PowerPoint?

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

How to copy a chart from Excel to PowerPoint?

excelvbaexcel-vbapowerpoint

提问by Aleksei Nikolaevich

Is there any way I could copy a chart from an excel spreadsheet to a powerpoint, preserving the original formatting and embedding the data? There was already a questionabout copy pasting charts programmatically. However, there was nothing said about data embedding

有什么方法可以将图表从 excel 电子表格复制到 PowerPoint 中,保留原始格式并嵌入数据?已经有一个 关于以编程方式复制粘贴图表的问题。然而,没有提到数据嵌入

The biggest problem is embedding the data. As far as I know data embedding requires recreating the chart from the beginning in the power point. (PS: By embedding i do not mean linking to an external excel file.)

最大的问题是嵌入数据。据我所知,数据嵌入需要从电源点的开始重新创建图表。(PS:通过嵌入我并不意味着链接到外部 excel 文件。)

回答by David Zemens

What you need to do is invoke the PasteSpecial "Keep Source Formatting and Embed Workbook".

您需要做的是调用 PasteSpecial“保留源格式和嵌入工作簿”。

enter image description here

在此处输入图片说明

Assume you have already created the charts, and the slides, placeholders/etc., and you have already copied the chart and navigated to the destination slide, and that you have an object like PPTAppto represent the PowerPoint.Applicationobject.

假设您已经创建了图表、幻灯片、占位符/等,并且您已经复制了图表并导航到目标幻灯片,并且您有一个对象PPTApp来表示该PowerPoint.Application对象。

Instead of using the Shapes.PasteSpecialmethod, you can do this:

Shapes.PasteSpecial您可以这样做,而不是使用该方法:

PPTApp.CommandBars.ExecuteMso "PasteExcelChartSourceFormatting"

This does not create a link to the Excel document, it embeds a local copy of the document in the PowerPoint Presentation. I think I understand this is your requirement.

这不会创建 Excel 文档的链接,而是在 PowerPoint 演示文稿中嵌入文档的本地副本。我想我明白这是你的要求。

Update from comments

从评论更新

Documentation on the ExecuteMsomethod:

ExecuteMso方法的文档:

http://msdn.microsoft.com/en-us/library/office/ff862419.aspx

http://msdn.microsoft.com/en-us/library/office/ff862419.aspx

Downloadable document containing the idMSOparameters for each Office Application:

包含idMSO每个 Office 应用程序参数的可下载文档:

http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=6627

http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=6627

NOTE:If you want to do something with the pasted chart after .ExecuteMsoyou may need to check if the shape is already pasted because .ExcecuteMsois asynchronous (the macro doesn't know when it's finished). Another question shows you how to wait for its completion.

注意:如果您想对粘贴的图表做一些事情,.ExecuteMso您可能需要检查形状是否已经粘贴,因为它.ExcecuteMso是异步的(宏不知道它何时完成)。另一个问题告诉你如何等待它的完成