The issue of handling Powerpoint events with C#

Wen Kong 1 Reputation point
2021-03-23T01:39:42.147+00:00

I have the follow code:

===============================================================
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using PPTApplication = Microsoft.Office.Interop.PowerPoint.Application;
using PPTPresentation = Microsoft.Office.Interop.PowerPoint.Presentation;

namespace PPTTEST
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private DispatcherTimer timer = new DispatcherTimer();

    public PPTApplication MyApplication { get; set; }

    public StatusEnum Status { get; set; } = StatusEnum.Invalid;

    public MainWindow()
    {
        InitializeComponent();
        timer.Tick += new EventHandler(Timer_Tick);
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (Status == StatusEnum.Invalid)
        {
            try
            {
                MyApplication = (PPTApplication)Marshal.GetActiveObject("PowerPoint.Application");
                Status = StatusEnum.Ready;
                MyApplication.SlideShowBegin += OnSlideShowBegin;
                MyApplication.SlideShowEnd += OnSlideShowEnd;
            }
            catch
            {
                Status = StatusEnum.Invalid;
                return;
            }
        }
        try   
        {
            ///confirm the ppt is closed or not
            string  ss = MyApplication.Caption;
            if (Status == StatusEnum.Ready)
            {
                ///handle the job1
            }
            else
            {
                ///handle the job2              
            }
        }
        catch(COMException ex)
        {
            Status = StatusEnum.Invalid;
            MyApplication = null;            
            return;
        }
    }

    private void OnSlideShowEnd(PPTPresentation Pres)
    {
        if (Status == StatusEnum.Running)
        {
            Status = StatusEnum.Ready;              
        }
    }

    private void OnSlideShowBegin(SlideShowWindow SlideShowWindow)
    {
        if (Status == StatusEnum.Ready)
        {
            Status = StatusEnum.Running;
        }
    }

    public enum StatusEnum
    {
        Invalid, Ready, Running
    }
}

}

However, it faces some problems. When I running the code, then open the ppt and press F5 quickly. The event OnSlideShowBegin can't be trigger. But if I wait for at least 5 second before press F5, the event OnSlideShowBegin can be trigger. What is this reason?

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,651 Reputation points
    2021-03-23T07:38:58.127+00:00

    Hi WenKong-1218,
    >> string ss = MyApplication.Caption;
    Based on your code, I didn't find that you check that the ppt is closed or not anywhere.
    You can try to confirm it before triggering OnSlideShowBegin event.

    if (ss==" your application caption" &&Status == StatusEnum.Init)  
    

    And as Viorel said, using a timer with smaller interval to test:

    timer.Interval = TimeSpan.FromMilliseconds(10);  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.