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 :(
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With