Share via

Presentation Save Events

Anonymous
2011-03-08T03:07:03+00:00

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?

Microsoft 365 and Office | PowerPoint | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Anonymous
    2011-03-12T14:15:40+00:00

    From what I can tell there doesn't appear to be a direct way to tell the difference, as we can only work with the events themselves - not the chain of events that lead to the event being invoked. You may just have to bite the bullet and take the performance hit, as that is far less important that the overall stability of your program.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-03-11T18:38:23+00:00

    I guess that's possible, but it is really fragile and only works for my code when I know that I am exporting.  What happens if the user does something in PowerPoint that unknowingly triggers an export (like saving the slides to jpegs)?  Then without my boolean flag set, my code will crash.

    It destroys encapsulation to use a global variable like this across event handler invocations (especially ones that I don't trigger) and don't believe that Microsoft didn't think of this.  They just didn't document it.

    My main question still remains: What is the recommended way to modify the customXMLParts without forcing the whole pptx file to write to disk twice?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-03-08T04:09:06+00:00

    Are you able to export without errors using the PresentationSave event? If so, why not set a boolean flag whenever you call the Export function in your code to ignore the BeforeSave event and use the PresentationSave event instead? You could then instruct the normal save sequence to do the opposite.

    Was this answer helpful?

    0 comments No comments
  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more