There was an interesting question in the forums the other day that piqued my interest. How to create a chart in a WordOpenXML document that will function in Word 2000-2003 (using the converter in the Compatibiltiy Pack), in Word 2007 and in Word 2010. We actually figure it out in less than 24 hours! (That was a day the lightbulb did come on.)
The key to tracking this down was fifteen years’ experience in the VBA versions of Office, research done for “Word Programmierung – das Handbuch” and application of the research principles mentioned an earlier blog post.
From experience, I knew that up until Office 2007 charts were embedded in Word documents (and PowerPoint slides, for that matter) using OLE: Object Linking and Embedding. The actual engine for creating, editing and displaying the charts was the one used by Excel. A Chart object was actually an Excel workbook containing an Excel Chart sheet and an Excel worksheet for the data. Double-clicking the chart object started Excel up, invisibly, in the background.
As far as Word was concerned, the chart was a graphics object, same as a picture. Behind the scenes, it was maintained by Word’s technology for dynamic content, a field. To be more exact, an EMBED field. When Word (the “OLE client”) encounters such a field, it knows to look up the class name of the object (such as Excel.Chart.8) in the Windows Registry in order to start up the related OLE Server program that handles the actual functionality.
With the advent of Office 2007, Microsoft completely revamped Excel’s charting engine as the possibilities provided by the old one just weren’t meething the requirements of modern business life. At the same time, the new file formats – Office Open XML – were being introduced. So charts are no longer just a “black box”, a binary blob in the old binary file format. Now they’re defined by their own Open XML vocabulary and can be integrated into any Office Open XML file, be it Word, PowerPoint or Excel. When in-place editing is activated the data no longer appears on a sheet behind the chart; instead, an Excel window is opened up next to the host Office application.
So, where’s the common ground? How to generate a chart using Open XML that can also be used in the old binary versions of Word?
When I first read the question, I thought it couldn’t be done. At best, I feared, one could insert a binary OLE object into the Office 2010 document. But create one based on Open XML – I was skeptical. And why should Microsoft go to all that trouble in order to have forwards compatibility? Backwards compatibility, yes, but forwards?
After some thought, the obvious starting point was to fire up Word 2010 and use Insert/Text/Object/Object , selecting “Microsoft Excel Chart” to emulate creating a chart in Word 2003. The question was, would this insert an OLE object (as I expected) or something else?
My first surprise was that it wasn’t the old standard red-blue-yellow chart with North-West-East data with which I was so familiar from the old days.
The second surprise came when I asked Word to give me the WordOpenXML (in the VBA Immediate Window:
Selection.Text=ActiveDocument.Content.WordOpenXML). The XML in the
w:body element was looking very Office Open XML-ish. And when I looked further I found a Part in the XML package for the chart that contained fairly standard ChartML.
Now, I’m not (yet) particularly conversant with what all is involved with ChartML, but the person asking the question is and discovered that the key point is where and how in the Package the chart is saved.
Word can have an oleObject relationship type that is stored in an embeddings “folder”. The actual object saved in the folder is a “normal” Open XML Excel Chart. But when the converter in the Compatibility Pack encounters this, it apparently knows to convert the object to an Office 2003 Excel chart!
In contrast, the Word Open XML package with a “normal” Office 2010 chart has this relationship:
The chart itself is stored in a charts folder, while the separate worksheet with the data is stored in the embeddings folder.
The situation in Office 2013 looks very much the same as in 2010, by the way, but the colors used for the standard chart have changed yet again…
Charting is a fascinating topic, no matter which version of Office. In future posts I’ll look at manipulating charts through the object models and, eventually, using Open XML.