Partager via


Presentation.Saved property in PowerPoint returns false for a new, un-modified presentation

Suppose you are developing a PowerPoint add-in and want to use the PresentationCloseEvent to present users with a custom dialog to save the presentation only if the new presentation contains valid changes to be saved.

Here is a code snippet from one such C# add-in to explain the scenario programmatically (for the programming geeks ) -

public partial
class ThisAddIn

{

void Application_AfterNewPresentation(PowerPoint.Presentation Pres)

        {

           Pres.Saved = MsoTriState.msoTrue;

        }

void Application_PresentationClose(Microsoft.Office.Interop.PowerPoint.Presentation Pres)

        {

            MessageBox.Show(Pres.Saved.ToString());

              //Pres.Path will tell if it’s a new unsaved presentation     

           if (String.IsNullOrEmpty(Pres.Path) && Pres.Saved == MsoTriState.msoFalse)

            {

                DialogResult nResponse = MessageBox.Show("Do you want to save your presentation" ,"PPT AddIn", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                 if (nResponse == DialogResult.Yes)

                {

                    Pres.Save();

                }

            }

         }

 

In order to test this -

-Open PowerPoint.

-Click somewhere inside the new slide.

-Save and close the presentation - at this point Pres.Saved might be msoTrue, so you won't see the prompt to save from the AddIns PresentationClose handler.

-Now open a new presentation by clicking on File | New | Blank presentation

-File | Close => Pres.Saved is msoFalse !!!

However, due to a known PowerPoint issue, accessing the ActivePresentation.Saved property on a new, unsaved presentation, always results in the ActivePresentation.Saved property returning msoFalse, regardless of whether the new presentation contains any valid changes to be saved.

This is a known bug and has been a legacy behavior with PowerPoint for quite some time. (since PPT 11).

The reason the Presentation.Saved returns incorrect value for new presentations is due to Presentation’s document property storage being created. The PowerPoint code does a lazy initialization of the property storage, thus resulting into incorrectly flagging the saved property.

The approach to work around this problem is to capture the Application::AfterNewPresentation event and explicitly access the BuiltInDocumentProperties so that the property storage does get created, and then set the Pres.Saved to msoTrue.

The following C# code snippet demonstrates the approach for the workaround programmatically -

        void Application_AfterNewPresentation(PowerPoint.Presentation Pres)

        {

            //Access the BuiltInDocumentProperties so that the property storage does get created.

            object o = Pres.BuiltInDocumentProperties;

            Pres.Saved = Microsoft.Office.Core.MsoTriState.msoTrue;

         }

 

 Hope this helps!