使用单击动画以编程方式推进Powerpoint幻灯片放映

时间:2011-12-06 15:57:30

标签: c# animation powerpoint office-interop

我要做的是从我的WPF应用程序控制一个Powerpoint演示文稿。 使用此问题的代码: C# - way to programmatically advance Powerpoint slide show? 它适用于普通幻灯片。

但是一旦我看到带有鼠标点击触发动画的幻灯片,它就无法正常工作。 当进入这样的幻灯片时,它将按预期显示,但是当我调用objPres.SlideShowWindow.View.Next()时,它什么都不做,并且在第二次或第三次单击后,它直接进入下一张幻灯片,没有动画

奇怪的是: 当我通过Timer调用objPres.SlideShowWindow.View.Next()时,它可以工作!动画正在按预期运行。

这是我的代码:

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();
}

我确信这很容易,我忽视了一些事情。但是我在这方面已经敲了很长时间:(

1 个答案:

答案 0 :(得分:4)

我在MSDN论坛上找到了解决问题的方法: 使用按钮时,动画无法正常播放,因为PPT没有焦点。当我在调用oSlideShowView.Next()之前激活SlideShowWindows时,它可以工作:

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