Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
717 views
in Technique[技术] by (71.8m points)

vba - How to copy a chart from Excel to PowerPoint?

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 question about copy pasting charts programmatically. However, there was nothing said about data embedding

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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 PPTApp to represent the PowerPoint.Application object.

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

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.

Update from comments

Documentation on the ExecuteMso method:

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

Downloadable document containing the idMSO parameters for each Office Application:

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 .ExecuteMso you may need to check if the shape is already pasted because .ExcecuteMso is asynchronous (the macro doesn't know when it's finished). Another question shows you how to wait for its completion .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...