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 (Critical Path Training) make an update to one of our courses, we version the course and include the version in the header/footer of our slides & lab files. The easiest way to do this in PowerPoint is to use the Insert » Header & Footer (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 (video demo here) that 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:
- Within the /ppt/presentation.xml part you'll find all the slides.
- Each slide has it's own part (/ppt/slides/slide12.xml).
- 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).
- 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.
- 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:
As you can see if 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:
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 (as it's pretty massive), but you can see how it generates a new Shape object 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 & footers in about 15 seconds where the tedious manual task used to take close to an hour!