Pieter van der Westhuizen

How to create PowerPoint add-in in C#: scheduled PPT presentation

Microsoft PowerPoint is a long-time member of the Microsoft Office family of products, and you’ve probably seen a fair share of PowerPoint presentations. Although PowerPoint has been part of Microsoft Office since 1990, we’ve never really written any articles on writing add-ins for it using Add-in Express.

Eugene came up with a very interesting idea for an add-in for PowerPoint that will enable the user to schedule when the presentation should be shown and even which slides should be shown when and for how long. How he comes up with these innovative ideas, I do not know : )

So let’s get started and create a smart plugin that will work in PowerPoint 2010 and 2007.

Creating a PowerPoint add-in in Visual Studio

Create a new PowerPoint COM Add-in project in Visual Studio 2010 using Add-in Express for Office and .net.

Creating a new COM Add-in project in Visual Studio 2010

Select Visual C# as our programming language (VB.NET and C++ are also supported) and Microsoft Office 2007 as the minimum supported Office version.

Selecting Visual C# as our programming language and Microsoft Office 2007 as the minimum supported Office version

Next, when prompted, select Microsoft PowerPoint as the only supported application.

Selecting PowerPoint as the only supported application

Adding the PowerPoint add-in UI components

Add a new ADX PowerPoint Task Pane item to our project by selecting Add New Item from the Visual Studio Project menu, once the wizard was completed.

Adding a PowerPoint task pane

We’ll use this task pane to set when each slide needs to be shown and for how long. The design for the pane should resemble the following:

Designing a custom PowerPoint task pane

Switch back to the AddinModule designer and add a new ADXPowerPointTaskPanesManager component. Add a new item to the ADXPowerPointTaskPanesManager’s Items collection and set the following properties on it:

  • AlwaysShowHeader = True
  • Position = Right
  • TaskPaneClassName = <YourProject>.SchedulePane

In this way, our custom PowerPoint task pane will show up in the right side of the window and it will have a standard header.

Next, add a new ADXRibbonTab control to the AddinModule designer surface, then add a Ribbon group and a Ribbon button to it. The design should look similar to the following image:

A custom Ribbon group and a button in the Add-in Express visual designer

Next, add a new class called SlideSchedule.cs to the project.

public class SlideSchedule
{
    public int SlideIndex { get; set; }
    public DateTime ShowAt { get; set; }
    public int ShowFor { get; set; }
}

This class will hold the schedule for each slide. Next, switch back the design for the ADX PowerPoint Task Pane we’ve added earlier and add the following code to the Save buttons’ Click event:

private void buttonSave_Click(object sender, EventArgs e)
{
    ScheduledPPT.AddinModule module = null;
    PowerPoint._Application app = null;
    PowerPoint.DocumentWindow activeWindow = null;
    PowerPoint.View activeView = null;
    PowerPoint.Slide activeSlide = null;
    SlideSchedule slideSchedule = null;
 
    try
    {
        module = AddinExpress.MSO.ADXAddinModule.CurrentInstance as
            ScheduledPPT.AddinModule;
        app = (PowerPoint._Application)this.PowerPointAppObj;
        activeWindow = app.ActiveWindow;
        activeView = activeWindow.View;
        activeSlide = (PowerPoint.Slide)activeView.Slide;
 
        slideSchedule = module.Schedule.FirstOrDefault(
            s => s.SlideIndex == activeSlide.SlideIndex);
        if (slideSchedule != null)
        {
            slideSchedule.ShowAt = dateTimePickerStart.Value;
            slideSchedule.ShowFor = Convert.ToInt32(textBoxMinutes.Text);
        }
        else
        {
            module.Schedule.Add(new SlideSchedule
            {
                SlideIndex = activeSlide.SlideIndex,
                ShowAt = dateTimePickerStart.Value,
                ShowFor = Convert.ToInt32(textBoxMinutes.Text)
            });
        }
        RefreshListView(module.Schedule);
    }
    finally
    {
        if (activeSlide != null)
            Marshal.ReleaseComObject(activeSlide);
        if (activeView != null)
            Marshal.ReleaseComObject(activeView);
        if (activeWindow != null)
            Marshal.ReleaseComObject(activeWindow);
    }
}

The code above adds the current slide’s index and schedule information to a public variable called “Schedule”, which we’ve declared in our AddinModule class.

Next, switch to the AddinModule designer and add two Timer controls called timerSlide and timerSchedule.

The timerSchedule timer will run and check which slides have been scheduled and will then display the slides as required.

PowerPoint.SlideShowWindow slideShowWindow = null;
 
private void timerSchedule_Tick(object sender, EventArgs e)
{
    PowerPoint.DocumentWindow activeWindow = null;
    PowerPoint.Presentation activePresentation = null;
    PowerPoint.View activeView = null;
    PowerPoint.SlideShowSettings slideShowSettings = null;
    PowerPoint.SlideShowView slideShowView = null;
    SlideSchedule slideSchedule = null;
 
    try
    {
        slideSchedule = Schedule.FirstOrDefault(
			s => s.ShowAt.Hour == DateTime.Now.Hour && 
				s.ShowAt.Minute == DateTime.Now.Minute);
        if (slideSchedule != null)
        {
            activePresentation = PowerPointApp.ActivePresentation;
            activeWindow = PowerPointApp.ActiveWindow;
            activeView = activeWindow.View;
 
            slideShowSettings = activePresentation.SlideShowSettings;
 
            slideShowSettings.EndingSlide = slideSchedule.SlideIndex;
            activePresentation.Save();
            slideShowWindow = slideShowSettings.Run();
 
            slideShowView = slideShowWindow.View;
            slideShowView.GotoSlide(slideSchedule.SlideIndex);
 
            timerSlide.Enabled = true;
            timerSlide.Interval = slideSchedule.ShowFor * 60000;
            timerSlide.Start();
            timerSchedule.Stop();
        }
    }
    finally
    {
        if (slideShowSettings != null)
            Marshal.ReleaseComObject(slideShowSettings);
        if (activeView != null)
            Marshal.ReleaseComObject(activeView);
        if (activePresentation != null)
            Marshal.ReleaseComObject(activePresentation);
        if (activeWindow != null)
            Marshal.ReleaseComObject(activeWindow);
        if (slideShowView != null)
            Marshal.ReleaseComObject(slideShowView);
    }
}

It will also start timerSlide which in turn will display the slide for the pre-set amount of time. The code for timerSlide’s Tick event, would be as follows:

private void timerSlide_Tick(object sender, EventArgs e)
{
    PowerPoint.SlideShowView slideShowView = null;
    try
    {
        timerSlide.Stop();
        slideShowView = slideShowWindow.View;
        slideShowView.Exit();
        timerSchedule.Start();
    }
    finally
    {
        if (slideShowView != null)
            Marshal.ReleaseComObject(slideShowView);
    }
}

Finally, to start the whole process, we need to add code to the Ribbon button we’ve added earlier on the OnClick event:

private void runWithScheduleRibbonButton_OnClick(object sender, 
	IRibbonControl control, bool pressed)
{
    timerSchedule.Enabled = true;
    timerSchedule.Start();
}

There you go, the user can now open his PowerPoint presentation, select each slide and specify when and for how long each slide should run.

A scheduled presentation in PowerPoint 2010

Thank you for reading! Until next time, keep coding!

Available downloads:

These sample COM Add-ins were developed using Add-in Express for Office and .net:

Scheduled PowerPoint presentation: C# sample

2 Comments

  • http://1.gravatar.com/avatar/d8369c8d7a01dd173535f864cce07fd0?s=32&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Danh says:

    Dear Pieter van der Westhuizen!
    I amd coding to create slideshow of powerpoint in C#. I want to some slides (example: slide 1, 3, 5 are loop) that will be “loopuntilstoped”. How do I code?
    Thank you!

  • http://0.gravatar.com/avatar/e1a4c2b21a5186e0b27c1c601f418b76?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Pieter van der Westhuizen says:

    Hi Danh,

    Build the sequence of slides in a similar manner as I’ve done in this example. You could then write some logic to automatically go back to the first slide as soon as the last slide in the sequence is shown. So you could use the example in this blog post as is, but at the end, don’t stop the timer and then call the slideShowView.GotoSlide method again and set it to slide number of the first slide.

    Hope this helps!

Post a comment

Have any questions? Ask us right now!