Link to home
Start Free TrialLog in
Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on

Capturing the "SlideShowNextSlide" Event in a PowerPoint Presentation via C#

Hi, I'm using Visual Studio 2010 C#. -- I have the code shown below, which opens a PowerPoint file and places the slides in "presentation" mode. -- That part works.

My Form is nothing but a basic Windows Form with a button on it called the default "button1" name.
You will also need to add the Microsoft.Office.Interop.PowerPoint reference in your Solution Explorer.

The problem I'm having is capturing a change of slide event, which is called ("SlideShowNextSlide"). To test this, I am simply writing a simple message to the console with Console.WriteLine. -- This is the part that does not work.

Basically, what I would like to do is to capture one of the events below:

- a mouse click on the presentation slide, which advances the position to the next slide.
- a key-down event, which advances the position to the next slide.
- a wireless clicker that issues a "next slide" signal, which advances the position to the next slide.
- any "next slide" event.

I think my problem is that I'm not coding the SlideShowNextSlide EventHandler properly, which is based on the documentation I've reviewed regarding this matter.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Office.Interop;
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

namespace ppAPPContoller
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileToOpen = "";
        Stopwatch stopwatch = new Stopwatch();
        OpenFileDialog ofd = new OpenFileDialog();

        Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
        Microsoft.Office.Interop.PowerPoint.SlideShowWindows objSSWs;
        Microsoft.Office.Interop.PowerPoint.SlideShowSettings objSSS;
        Microsoft.Office.Interop.PowerPoint.SlideShowView oSlideShowView;

        private void button1_Click(object sender, EventArgs e)
        {
            ofd.Filter = "Powerpoint|*.ppt;*.pptx";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fileToOpen = ofd.FileName;
                var pres = ppApp.Presentations;
                var file = pres.Open(fileToOpen, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);
                var objSlides = file.Slides;

                //Run the Slide Show
                objSSS = file.SlideShowSettings;
                objSSS.Run();
                objSSWs = ppApp.SlideShowWindows;

                button1.Enabled = false;
                Console.WriteLine("Opening in Presentation mode: " + ofd.SafeFileName);
                Console.WriteLine("Full Path: " + ofd.FileName);
            }
            stopwatch.Start();
        }

        // Code to capture SlideShowNextSlide Event
        private void InitPPT()
        {
            this.ppApp.SlideShowNextSlide += this.SlideShowNextSlide;
        }

        private void SlideShowNextSlide(PowerPoint.SlideShowWindow Wn)
        {
            Console.WriteLine("Slide changed....");
        }


        private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            ppApp.Quit();
            stopwatch.Stop();
        }

    }
}

Open in new window

Thanks,
Fulano
Avatar of Jamie Garroch (MVP)
Jamie Garroch (MVP)
Flag of United Kingdom of Great Britain and Northern Ireland image

I know how to use these events in VB/VBA but not C#. This is the 3-step process which I assume you need to replicate in C#:

https://msdn.microsoft.com/en-us/library/office/ff746018.aspx
Avatar of Mr_Fulano

ASKER

Yeah...a lot of the examples I've been reviewing are VBA, but VBA is not my forte. I need to be able to do this in C#. Thanks for your response.

Do you think you can do it in VB? I think VB can be converted to C# with little effort.
ASKER CERTIFIED SOLUTION
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Daniel, yes indeed...!!! That is the same solution I just came up with.  -- WOW, I had been working on this one for almost 2 days!  -- Thank you for confirming that I was on track!!!   : )

private void MonitorSlideChange()
        {
            ppApp.SlideShowNextSlide += ppApp_SlideShowNextSlide;
        }

private void ppApp_SlideShowNextSlide(SlideShowWindow Wn)
{
   //Do Stuff....
}
Great solutions!!!