question

WenKong-1218 avatar image
0 Votes"
WenKong-1218 asked DanielZhang-MSFT commented

The issue of handling Powerpoint events with C#

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?


dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe use a timer with smaller interval, such as 50 ms, and once you detect that ‘MyApplication =…’ works successfully, execute immediately, inside the same tick, ‘MyApplication.SlideShowBegin += OnSlideShowBegin’.

Perhaps you can also check some status of MyApplication, because if the PowerPoint application and slideshow are already running, the SlideShowBegin event will not happen.


0 Votes 0 ·

I update the code as above.
I try to change the interval to 50ms. But it is not working. If I want to check the slideshow, What status can I check from MyApplication except the SlideShowBegin event?

0 Votes 0 ·

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT commented

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.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

With the statement >>string ss=MyApplication.Caption.
If the ppt is closed, it will cause COMException, so that I can confirmed all PPTs are closed.

A timer with smaller interval can't solve this problem. My powerpoint version is Powerpoint 2019.

0 Votes 0 ·

Hi @WenKong-1218,
What is your "PPTAppication "?
In oreder to reproduce the situation, please provide more code about your PPTAppication.
Best Regards,
Daniel Zhang

0 Votes 0 ·

It is the alias of "Microsoft.Office.Interop.PowerPoint.Application", I update the code and add the namespace.
And the version of "Microsoft.Office.Interop.PowerPoint" is 15.0.0.0. What's more, if I drag the window more than 2 second after opening the ppt, the event OnSlideShowBegin can be trigger.

0 Votes 0 ·
Show more comments