none
Powerpoint ActivePresentation.SaveAs Fails with COMException ErrorCode=-2147467259 RRS feed

  • Question

  • Can some one explain why the following source code throws Exception. The source code opens a SaveFileDialog and then Saves the files to the location specified by the User. 
    System.Runtime.InteropServices.COMException was unhandled by user code
      HelpLink=""
      Message=Presentation (unknown member) : Failed.
      Source=Microsoft Office PowerPoint 2007
      ErrorCode=-2147467259
      StackTrace:
           at Microsoft.Office.Interop.PowerPoint._Presentation.SaveAs(String FileName, PpSaveAsFileType FileFormat, MsoTriState EmbedTrueTypeFonts)
           at PowerPointAddIn1.ThisAddIn.PowerPointDocument.ppoint_PresentationBeforeSave(Presentation Pres, Boolean& Cancel) in C:\Users\howie.zhu\documents\visual studio 2010\Projects\PowerPointAddIn1\PowerPointAddIn1\ThisAddIn.cs:line 81
      InnerException: 
    Source Code:
        void ppoint_PresentationBeforeSave(Presentation Pres, ref bool Cancel)
                {
                    Microsoft.Office.Core.FileDialog fileDialog = ((Microsoft.Office.Interop.PowerPoint.Application)ppoint).get_FileDialog(MsoFileDialogType.msoFileDialogSaveAs);
                    fileDialog.Show();
                    String itemone = fileDialog.SelectedItems.Item(1);
                    ((Microsoft.Office.Interop.PowerPoint.Application)ppoint).ActivePresentation.SaveAs(itemone,
                        Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation, 
                        Microsoft.Office.Core.MsoTriState.msoTrue);
       }
    Friday, January 20, 2012 1:51 AM

Answers

  • Hello,

    PowerPoint doesn't allow saving in the PresentationBeforeSave event. To bypass this, in PresentationBeforeSave, you cancel the event(Cancel=true), start a timer (System.Windows.Forms.Timer), and perform Presentation.Save (Presentation.SaveAs) in the Tick event of the timer. Note that saving triggers PresentationBeforeSave and you'll need to use a flag. 


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.
    Friday, January 20, 2012 4:31 AM
  • Hi,

    I'm not sure I've got your point, but according to your previous thread:

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/ee09631f-a36d-4482-8162-4c91509fcff2

    It seems that you want to save all presentations within the current application by clicking a button which located on your custom ribbon, and while PowerPoint program is trying to save a presentation, you want to do some customizations for each presentation in event handler of PresentationBeforeSave event. Actually, I wrote some code maybe you want to have a look at:

            private Office.IRibbonUI ribbon;
            //declare a dictionary
            public Dictionary<string, PowerPoint.Presentation> SaveAsDictionary;
    
            public RibbonSaveAs()
            {
                SaveAsDictionary = new Dictionary<string, PowerPoint.Presentation>();
            }
    
            //callback for a custom button of ribbon
            public void button1_click(Office.IRibbonControl control)
            {
                Globals.ThisAddIn.Application.PresentationBeforeSave += new PowerPoint.EApplication_PresentationBeforeSaveEventHandler(Application_PresentationBeforeSave);
                foreach (PowerPoint.Presentation pre in Globals.ThisAddIn.Application.Presentations)
                {
                    try
                    {
                        //each time this line of code is executed, the PresentationBeforeSave event is trigged
                        pre.Save();
                    }
                    catch {  }               
                }
    
                Globals.ThisAddIn.Application.PresentationBeforeSave -= new PowerPoint.EApplication_PresentationBeforeSaveEventHandler(Application_PresentationBeforeSave);
    
                foreach (KeyValuePair<string, PowerPoint.Presentation> pair in SaveAsDictionary)
                {
                    pair.Value.SaveAs(pair.Key, PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Office.MsoTriState.msoTrue);
                }
            }
    
            //event handler for the PresentationBeforeSave event
            void Application_PresentationBeforeSave(PowerPoint.Presentation Pres, ref bool Cancel)
            {
                Cancel = true;
                Office.FileDialog fileDialog =Globals.ThisAddIn.Application.get_FileDialog(Office.MsoFileDialogType.msoFileDialogSaveAs);
                fileDialog.Show();
                String itemone = fileDialog.SelectedItems.Item(1);
                SaveAsDictionary.Add(itemone, Pres);
    
                //do your customization here
            }
    

    I hope this helps.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 20, 2012 9:10 AM
    Moderator

All replies

  • Hello,

    PowerPoint doesn't allow saving in the PresentationBeforeSave event. To bypass this, in PresentationBeforeSave, you cancel the event(Cancel=true), start a timer (System.Windows.Forms.Timer), and perform Presentation.Save (Presentation.SaveAs) in the Tick event of the timer. Note that saving triggers PresentationBeforeSave and you'll need to use a flag. 


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.
    Friday, January 20, 2012 4:31 AM
  • Hi,

    I'm not sure I've got your point, but according to your previous thread:

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/ee09631f-a36d-4482-8162-4c91509fcff2

    It seems that you want to save all presentations within the current application by clicking a button which located on your custom ribbon, and while PowerPoint program is trying to save a presentation, you want to do some customizations for each presentation in event handler of PresentationBeforeSave event. Actually, I wrote some code maybe you want to have a look at:

            private Office.IRibbonUI ribbon;
            //declare a dictionary
            public Dictionary<string, PowerPoint.Presentation> SaveAsDictionary;
    
            public RibbonSaveAs()
            {
                SaveAsDictionary = new Dictionary<string, PowerPoint.Presentation>();
            }
    
            //callback for a custom button of ribbon
            public void button1_click(Office.IRibbonControl control)
            {
                Globals.ThisAddIn.Application.PresentationBeforeSave += new PowerPoint.EApplication_PresentationBeforeSaveEventHandler(Application_PresentationBeforeSave);
                foreach (PowerPoint.Presentation pre in Globals.ThisAddIn.Application.Presentations)
                {
                    try
                    {
                        //each time this line of code is executed, the PresentationBeforeSave event is trigged
                        pre.Save();
                    }
                    catch {  }               
                }
    
                Globals.ThisAddIn.Application.PresentationBeforeSave -= new PowerPoint.EApplication_PresentationBeforeSaveEventHandler(Application_PresentationBeforeSave);
    
                foreach (KeyValuePair<string, PowerPoint.Presentation> pair in SaveAsDictionary)
                {
                    pair.Value.SaveAs(pair.Key, PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Office.MsoTriState.msoTrue);
                }
            }
    
            //event handler for the PresentationBeforeSave event
            void Application_PresentationBeforeSave(PowerPoint.Presentation Pres, ref bool Cancel)
            {
                Cancel = true;
                Office.FileDialog fileDialog =Globals.ThisAddIn.Application.get_FileDialog(Office.MsoFileDialogType.msoFileDialogSaveAs);
                fileDialog.Show();
                String itemone = fileDialog.SelectedItems.Item(1);
                SaveAsDictionary.Add(itemone, Pres);
    
                //do your customization here
            }
    

    I hope this helps.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 20, 2012 9:10 AM
    Moderator
  • Hello, i have the same problem. when i call save method the powerpoint get me an exception

    Presentation (Unknown Member)

    how can i do to call save method in "PresentationBeforeSave" event??

    give me a exemple, please.

    • Proposed as answer by Plínio Luiz Wednesday, October 3, 2012 1:19 PM
    • Unproposed as answer by Plínio Luiz Wednesday, October 3, 2012 1:19 PM
    Wednesday, October 3, 2012 1:19 PM