Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a macro to a Word 2010 or PowerPoint 2010 chart?

In PowerPoint 2010 or Word 2010, when I choose Insert -> Chart, it creates a new chart with an Excel worksheet for the data.

If I add a macro to the Excel worksheet, this seems to be discarded when I close the worksheet and re-open it.

However, it is possible to have a chart with macros, because I also have some Word documents & PowerPoint presentations that I created in Office 2003, which had embedded Excel charts with macros. When I converted those to Office 2010, they look just like "normal" Office 2010 charts, but the macros are preserved.

It looks to me like the embedded chart that gets created when you choose Insert -> Chart is in the "pptx" format rather than the "pptm" format, and so macros are not saved.

(If I query ActiveWorkbook.FileFormat, I get "51", which is "Open Xml Spreadsheet"; this is indeed "pptx", as opposed to "Open Xml Spreadsheet with Macros", which is "52").

How can I insert a chart with the "pptm" behavior? Or change the behavior of an existing chart?


NOTE: I do not want to insert a chart "object", since this means that the chart is not editable within the host application (it merely inserts an Excel chart that you need to "open" in order to edit it).


Let me give you a head start... I can achieve what I want by the following method:

  1. Create a new document in Word (or PowerPoint).
  2. Save the document as a 97-2003 document (which forces it into compatibility mode).
  3. Insert a chart using Insert->Object->Microsoft Excel Chart. This embeds an old-style Excel chart.
  4. Open the embedded chart by right-click->Open, and add a macro.
  5. Close the chart, then choose File->Info and use the Compatibility Mode "Convert" button to convert the document into an Office 2010 document.

Now you have a "native" Word or PowerPoint 2010 chart, which you can edit directly in Word or PowerPoint. If you click on the chart and choose Chart Tools->Design->Edit Data, then the Excel worksheet that opens has the macros you created in step 4. Success!

Now, does anyone have a simpler way?

like image 487
Gary McGill Avatar asked May 29 '13 23:05

Gary McGill


2 Answers

In my opinion you could achieve what you need in the following way:

(unfortunately, I can't present it with pictures and exact English commands as I'm using local-not English-version of Office)

  • Choose Insert >> Object >> Object...
  • In Object window take first page (like Create new)
  • Search for Microsoft Excel Chart (or something similar) and press OK
  • You will get Workbook with two sheets as presented on the picture below

enter image description here

(there are chart editing feature available on the Ribbon)

  • Press Alt+F11 while you are in Chart edition like presented in the picture above. You will get IDE for Excel opened where you will find appropriate workbook.
  • Add new module and macro there
  • Now you could exit chart edition in Word and save document as *.docm
  • After you will reopen it, go to Chart Edition (double click) >> Alt+F11 >> you will find you macro there saved within the workbook with *docm extension.
like image 180
Kazimierz Jawor Avatar answered Oct 20 '22 05:10

Kazimierz Jawor


Now that the bounty has passed (with no better answers), I'll add the partial answer from my question, in order to be able to close the question.


  1. Create a new document in Word (or PowerPoint).
  2. Save the document as a 97-2003 document (which forces it into compatibility mode).
  3. Insert a chart using Insert->Object->Microsoft Excel Chart. This embeds an old-style Excel chart.
  4. Open the embedded chart by right-click->Open, and add a macro.
  5. Close the chart, then choose File->Info and use the Compatibility Mode "Convert" button to convert the document into an Office 2010 document.

Now you have a "native" Word or PowerPoint 2010 chart, which you can edit directly in Word or PowerPoint. If you click on the chart and choose Chart Tools->Design->Edit Data, then the Excel worksheet that opens has the macros you created in step 4. Success!

I'm still interested in being able to do this more directly...

like image 37
Gary McGill Avatar answered Oct 20 '22 06:10

Gary McGill