Using the OpenXML Productivity Tool

This Microsoft tool can help to automate a tedious process.

Andrew Connell

May 19, 2010

4 Min Read
ITPro Today logo in a gray background | ITPro Today

Recently I’ve been working on a tool to automate a tedious process I go through every once in a while that takes about 2-3 hours. When we make an update to one of our courses, we version the course and include the version in the header/footer of our slides and lab files.

The easiest way to do this in Microsoft PowerPoint is to use the Insert, Header & Footer tab (in the Ribbon) to let it make these changes in the notes layout for printing. Unfortunately, some slides seem to have their own notes slides that don’t respect the notes master template. You are supposed to be able to go into the actual notes view of a slide (View, Notes Page), right-click the layout and Notes Layout and in the dialog that appears select Reapply master.

Yeah… well, it would be nice if that worked because it doesn’t, unfortunately.

So I set out to fix this using the latest release of the OpenXML SDK 2.0. One of the things you get with the latest release is this killer Open XML Productivity Tool.  (Check out the YouTube video demo, too.)  The tool lets you explore a package (Office XML file format) and also generates the code needed to build the part you specify.

Using this tool, I quickly found that my problem slides, or those that had their own notes file customizations, were not reflecting what was in the notes master. Using the tool, you can see how a file is broken down, which Figure 1 shows.

  1. Within the /ppt/presentation.xml part you’ll find all the slides.

  2. Each slide has it’s own part (/ppt/slides/slide12.xml).

  3. If a slide has any notes customizations or has any presenter notes in the slide, you’ll see a notes slide (/ppt/notesSlides/notesSlide12.xml).

  4. Within this, you’ll see it should reference a notes master slide (/ppt/notesMasters/notesMaster1.xml). This is not a copy, but a reference to the notes master defined in the presentation.

  5. For each element in the notes slide you’ll find a shape nested in the shape tree. Shapes are things like the slide, notes field, header, footer, page number, and date.

So what I needed to do was basically walk through the entire presentation and for each slide:

  • If there wasn’t a notes slide, skip to the next slide. This is because without a notes slide, the master will get applied. But, if there is a notes slide, there are either header/footer customizations or speaker notes.

  • Remove all shape parts on the notes slide that are neither the slide or presenter notes.

  • Add back all the correct header & footer shapes with the proper content.

Enter the Open XML SDK 2.0. This actually isn’t that hard with the latest SDK. It all boils down to the following code in Figure 2.

As you can see, I first open the package and immediately start looping through all the slides. For each slide, I get a reference to the notes slide if it exists (if it doesn’t, I skip this slide). Next, using the LINQ support in the Open XML SDK 2.0 I get all shapes that are neither of type Body (the presenter notes) or SlideImage (the actual slide) and remove them.

The next step is to add back all the correct headers and footers, which you can see in Figure 3.

The code here is pretty straightforward. The magic is at the bottom, where I call methods that add stuff back to the slide. This is where the Open XML Productivity Tool really helps. What you do is find a shape in the presentation that has what you want, right-click it and get it to generate the code for you. I copied this code into one of these methods, added some parameters (such as the header, copyright, module number, etc.) and then modified the generated code to put my variable in the shape instead of the hard-coded piece.

I’ll spare you the code that was generated (it’s pretty massive), but you can see how it generates a new Shape object (see Figure 4), which you simply add back to the nodes slide.

And that’s it. I’ve now got a little utility that tears through a ton of presentations and sanitizes the headers and footers in about 15 seconds where the tedious manual task used to take close to an hour!

 

About the Author

Andrew Connell

http://www.andrewconnell.com/blog

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like