I'm developing an AddIn for PowerPoint 2010 on Windows 7SP1, using C# and I'm having some problems saving CustomXML parts into a PowerPoint presentation. I'm trying to figure out when each of the Application Events gets triggered when:
Initially, I added a handler to the PresentationSave event. When this event was called I would serialize necessary in-memory data structures and add them as CustomXML parts. This seemed to work but caused a really annoying problem where I would save the
presentation, and then if I closed it immediately (without making any more changes) PowerPoint would still ask if I wanted to save the presentation.
By tracing the Saved property of the presentation, I discovered that even when Saved == true at the top of the PresentationSave handler, once I manipulated the CustomXMLParts it would set Saved back to false before my handler exited, making
PowerPoint think that it has to save the presentation again. (Indeed, CustomXML changes are not actually wirtten to disk yet in this case)
I could fix this problem by calling Pres.Save() at the end of my handler. Remarkably, this does not cause infinite recursion and "fixes" the problem so that PowerPoint does not ask to save the presentation again when attempting to close immediately after
saving.
However, this appears to actually write the whole presentation to disk twice. Because the PowerPoint data (with embedded images) dwarfs the customXML I am adding, this generally causes a noticeable and undesirable doubling of the time it takes to save a
presentation.
So I started trapping the PresentationBeforeSave Event. If I modify the CustomXML here during the normal save process, now it "works", meaning that the CustomXML is stored correctly, the file is only saved once, and it does not ask to save when closing.
However, this caused the real trouble: The PresentationBeforeSave event is also called at other times, such as during Slide.Export(). It appears that Presentation BeforeSave may be called as many as 3 times during slide exporting without ever calling PresentationSave.
During these times, when I try to update the CustomXMLParts I get a COMException:
This command is not allowed because the document is locked.
Is there anyway to know when PresentationBeforeSave is called in this context, as opposed to when it is actually going to write everything to disk?
When is the safe time to modify the CustomXMLParts collection? And what is the recommended method for storing data in CustomXML without always saving everything twice?