Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advancing Powerpoint Slideshow programmatically with click animations

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 :(

like image 753
Malyngo Avatar asked Dec 06 '11 15:12

Malyngo


1 Answers

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();
}
like image 84
Malyngo Avatar answered Oct 17 '22 14:10

Malyngo