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

5 Comments

  • 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!

  • 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!

  • Dave Cordeiro says:

    Hello,
    I’m found this while trying to figure out how to create a scheduled sync action for an outlook plugin. Could these timers be used for that purpose? Also on the step “Next, switch to the AddinModule designer and add two Timer controls called timerSlide and timerSchedule.” I am confused because I cannot find how to add the Timer controls.

    Thanks,
    Dave

  • Andrei Smolin (Add-in Express Team) says:

    Hello Dave,

    If you use Add-in Express, then right-click AddinModule.cs or AddinModule.vb in your project and choose View Designer on the context menu. This opens the add-in module designer. Then open the Toolbox, see menu View | Toolbox in the IDE. In the Toolbox, expand the Components section. Alternatively, you can expand the All Windows Forms. Find the Timer entry and double-click it. This creates a new Timer component on the designer. Click the component just added and set its properties as required, see the Properties window.

  • Dave Cordeiro says:

    Thank you for the help Andrei!
    I was able to complete my project using this information.

    Thanks,
    Dave Cordeiro

Post a comment

Have any questions? Ask us right now!