6
votes

What I am trying to do is control a Powerpoint presentation from my WPF application. With the code from this question: C# - way to programmatically advance Powerpoint slide show? it is working quite well for normal slides.

But as soon as I get to a slide with an animation triggered by a mouseclick, it is not working as I would expect it to do. When going to such an slide, it will show up as expected, but when i call objPres.SlideShowWindow.View.Next(), it does nothing, and after the second or third click, it goes straight to the next slide, no animation.

The strange thing is: When I call objPres.SlideShowWindow.View.Next() via a Timer, it works! Animations are running as expected.

This is the code I have:

Microsoft.Office.Interop.PowerPoint.Application oPPT;
Microsoft.Office.Interop.PowerPoint.Presentations objPresSet;
Microsoft.Office.Interop.PowerPoint.Presentation objPres;
Microsoft.Office.Interop.PowerPoint.SlideShowView oSlideShowView;
Timer slidetest;

private void OpenPPT(object sender, RoutedEventArgs e)
{
    //Create an instance of PowerPoint.
    oPPT = new Microsoft.Office.Interop.PowerPoint.Application();
    // Show PowerPoint to the user.
    oPPT.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
    objPresSet = oPPT.Presentations;


    OpenFileDialog Opendlg = new OpenFileDialog();

    Opendlg.Filter = "Powerpoint|*.ppt;*.pptx|All files|*.*";

    // Open file when user  click "Open" button  
    if (Opendlg.ShowDialog() == true)
    {
        string pptFilePath = Opendlg.FileName;
        //open the presentation
        objPres = objPresSet.Open(pptFilePath, MsoTriState.msoFalse,
        MsoTriState.msoTrue, MsoTriState.msoTrue);

        objPres.SlideShowSettings.ShowPresenterView = MsoTriState.msoFalse;
        System.Diagnostics.Debug.WriteLine(objPres.SlideShowSettings.ShowWithAnimation);
        objPres.SlideShowSettings.Run();

        oSlideShowView = objPres.SlideShowWindow.View;


        slidetest = new Timer(4000);
        slidetest.AutoReset = false;
        slidetest.Elapsed += new ElapsedEventHandler(slidetest_Elapsed);
        slidetest.Start();

    }
}

void slidetest_Elapsed(object sender, ElapsedEventArgs e)
{
    // this works as expected
    oSlideShowView.Next();
}

private void OnNextClicked(object sender, RoutedEventArgs e)
{
    // this doesn't work, animations aren't shown at all.
    oSlideShowView.Next();
}

I am sure this is something easy and I am overlooking something. But I am banging my head on this for quite some time :(

1
In this case, might the problem be that there are timed animations that run automatically? If so, I suspect PPT wouldn't respond to oSlideShowView.Next unti it's done processing the animation.Steve Rindsberg
@steve-rindsberg no, there are no timed animations in the PPT I have tested. I have created a test project and uploaded it here: skydrive.live.com/…, if you want to take a look at it (there is also a test ppt in this zip, which just has two slides, with a click animation on the first slide).Malyngo
I'm not a C (sharp, flat, plus, minus) guy and am swamped at the moment. If I had time to look, it wouldn't help much anyhow. Sorry.Steve Rindsberg
great work. very usefulGobyDot

1 Answers

4
votes

I got the solution to my problem on the MSDN forums: When using the button, the animation does not play correctly because PPT does not have the focus. When I activate the SlideShowWindows before calling oSlideShowView.Next(), it works:

private void OnNextClicked(object sender, RoutedEventArgs e)
{
    oSlideShowView.Application.SlideShowWindows[1].Activate();
    oSlideShowView.Next();
}