简体   繁体   中英

PowerPoint 2010 events not fired in C#

I'm writing an application that uses the PowerPoint interop library for Office 2010. It's supposed to open PowerPoint, load a presentation, and wait for the user to save the presentation after making changes. I want the application to upload the presentation to a server once updated.

The problem I'm having is that the PresentationSave or PresentationBeforeSave events are not being triggered when the presentation is saved.

Here's the code I've used:

private void startPPT()
{
        app = new ApplicationClass();

        app.WindowState = PpWindowState.ppWindowMaximized;
        app.Visible = MsoTriState.msoTrue;

        app.PresentationBeforeSave += new EApplication_PresentationBeforeSaveEventHandler(app_PresentationBeforeSave);
        app.PresentationSave += new EApplication_PresentationSaveEventHandler(app_PresentationSave);

        Presentation ppt;

        //check if it's pptx or ppt and open accordingly
        FileInfo fi = new FileInfo(filename);
        if (fi.Extension == ".pptx")
        {
            //version 2007
            ppt = app.Presentations.Open2007(filename, MsoTriState.msoFalse, MsoTriState.msoFalse);
        }
        else
        {
            //version 2003 or older
            ppt = app.Presentations.Open(filename, MsoTriState.msoFalse, MsoTriState.msoFalse);
        }
}

    void app_PresentationSave(Presentation Pres)
    {
        MessageBox.Show("Saved");
    }

Any ideas why it isn't working?

Ok, it turns out I had to use a delegate in the event handler. Here's the code that worked, in case anyone needs it for reference:

    private void startPPT()
    {
    // as above
    }

    private delegate void CallBackPPTSaved(Presentation p);

    void app_PresentationSave(Presentation Pres)
    {
        this.Dispatcher.BeginInvoke(new CallBackPPT(PPTEventHandler), System.Windows.Threading.DispatcherPriority.Normal, Pres);
    }

    private void PPTEventHandler(Presentation p)
    {
        MessageBox.Show("Saved");
    }

It's because your code above isn't calling ppt.Save(); , it's just calling .Open() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM