C#:演示文稿保存不起作用

时间:2017-09-08 12:56:38

标签: c# asp.net .net powerpoint add-in

我正在尝试保存,修改从磁盘加载的现有powerpoint文件中的更改。我成功地打开了文件。 尝试使用saveas方法保存已编辑的(现有的)powerpoint文件时,会发现异常。

private void adxPowerPointAppEvents1_PresentationBeforeSave(object sender, ADXHostBeforeActionEventArgs e)
    {
        try
        {
            PowerPoint.Presentation pre = e.HostObject as 
            PowerPoint.Presentation;

            // cancel this operation
            e.Cancel = true;

            //save
            pre.SaveAs(pre.Name, 
    PowerPoint.PpSaveAsFileType.ppSaveAsDefault,Office.MsoTriState.msoTrue);
            MessageBox.Show("you will not see me due to exception");

     }    
     catch(Exception e){}

}

执行代码时,enter image description here

msword和msexcel中使用的方法无效。

如果有任何一种方法可以保存已编辑的文件,请帮助我..

1 个答案:

答案 0 :(得分:0)

解决了这个问题...... 就像单词一样,excel我们不能进行save()操作......如果我们从同一个事件处理程序(函数)调用save或saveas,则Powerpoint会抛出异常。因此,在PowerPoint之后,你必须在该事件处理程序之外调用SaveAs。调用它。

private void adxPowerPointAppEvents_PresentationBeforeSave(object sender, ADXHostBeforeActionEventArgs e)
        {
            PowerPoint.Presentation pre = e.HostObject as PowerPoint.Presentation;
            try
            {

                if (pre.Saved == Microsoft.Office.Core.MsoTriState.msoFalse)
                {
                    e.Cancel = true;

                    GCHandle handle = GCHandle.Alloc(pre.FullName);
                    IntPtr parameter = (IntPtr)handle;


                    this.SendMessage(MESSAGE_SAVE_PPT, parameter, IntPtr.Zero);
                    pre.Saved = Microsoft.Office.Core.MsoTriState.msoTrue;
                }
            }
            catch(Exception ex)
            {
                Log.Info("Exception while saving powerpoint : " + ex.StackTrace);
                MessageBox.Show(ex.Message);
            }
                return;
        }

private void AddinModule_OnSavePowerPointMessage(object sender, AddinExpress.MSO.ADXSendMessageEventArgs e)
        {
            if (e.Message == MESSAGE_SAVE_PPT)
            {
                PowerPoint.Presentation ppPre = null;
                try
                {
                    GCHandle handle = (GCHandle)e.WParam;
                    String fullName = (handle.Target as String);

                    ppPre = PowerPointApp.Presentations[fullName];
                    if (ppPre != null)
                    {
                        ppPre.Saved = Microsoft.Office.Core.MsoTriState.msoTrue;
                        //ppPre.SaveAs(ppPre.FullName, PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Microsoft.Office.Core.MsoTriState.msoTrue);
                        ppPre.Save();


                        Log.Info("Value of pre.name " + ppPre.Name);
                    }
                }
                catch (Exception exSavePpt)
                {
                    Log.Info("Exception while saving powerpoint : " + exSavePpt.StackTrace);
                }
            }
        }

实际上,在AddinModule_OnSavePowerPointMessage()中,我们不应该传递会创建com异常的COM对象。所以我们应该传递表示名称(ppt.fullname)并在第二种方法中创建指向同一对象的com引用。所以,现在没有任何麻烦我可以保存ppt文件...