The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Walkthrough: Automating an Application from a Custom Task Pane

This walkthrough demonstrates how to create a custom task pane that automates PowerPoint. The custom task pane inserts dates into a slide when the user clicks a MonthCalendar control that is on the custom task pane.

Applies to: The information in this topic applies to application-level projects for the following applications: Excel 2007 and Excel 2010; InfoPath 2007 and InfoPath 2010; Outlook 2007 and Outlook 2010; PowerPoint 2007 and PowerPoint 2010; Word 2007 and Word 2010. For more information, see Features Available by Office Application and Project Type.

Although this walkthrough uses PowerPoint specifically, the concepts demonstrated by the walkthrough are applicable to any applications that are listed above.

This walkthrough illustrates the following tasks:

  • Designing the user interface of the custom task pane.

  • Automating PowerPoint from the custom task pane.

  • Displaying the custom task pane in PowerPoint.


Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Working with Settings.

You need the following components to complete this walkthrough:

The first step is to create an add-in project for PowerPoint.

To create a new project

  • Create a PowerPoint add-in project with the name MyAddIn, by using the PowerPoint Add-in project template. For more information, see How to: Create Office Projects in Visual Studio.

    Visual Studio opens the ThisAddIn.cs or ThisAddIn.vb code file and adds the MyAddIn project to Solution Explorer.

There is no visual designer for custom task panes, but you can design a user control with the layout you want. Later in this walkthrough, you will add the user control to the custom task pane.

To design the user interface of the custom task pane

  1. On the Project menu, click Add User Control.

  2. In the Add New Item dialog box, change the name of the user control to MyUserControl, and click Add.

    The user control opens in the designer.

  3. From the Common Controls tab of the Toolbox, drag a MonthCalendar control to the user control.

    If the MonthCalendar control is larger than the design surface of the user control, resize the user control to fit the MonthCalendar control.

The purpose of the add-in is to put a selected date on the first slide of the active presentation. Use the DateChanged event of the control to add the selected date whenever it changes.

To automate PowerPoint from the custom task pane

  1. In the designer, double-click the MonthCalendar control.

    The MyUserControl.cs or MyUserControl.vb file opens, and an event handler for the DateChanged event is created.

  2. Add the following code to the top of the file. This code creates aliases for the Microsoft.Office.Core and Microsoft.Office.Interop.PowerPoint namespaces.

    using Office = Microsoft.Office.Core;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
  3. Add the following code to the MyUserControl class. This code declares a Shape object as a member of MyUserControl. In the following step, you will use this Shape to add a text box to a slide in the active presentation.

    private PowerPoint.Shape textbox;
  4. Replace the monthCalendar1_DateChanged event handler with the following code. This code adds a text box to the first slide in the active presentation, and then adds the currently selected date to the text box. This code uses the Globals.ThisAddIn object to access the object model of PowerPoint.

    private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
            if (textbox != null)
            PowerPoint.Slide slide =
            textbox = slide.Shapes.AddTextbox(
                50, 100, 600, 50);
            textbox.TextFrame.TextRange.Text = e.Start.ToLongDateString();
            textbox.TextFrame.TextRange.Font.Size = 48;
            textbox.TextFrame.TextRange.Font.Color.RGB =
        catch (Exception ex)
  5. In Solution Explorer, right-click the MyAddIn project and then click Build. Verify that the project builds without errors.

To display the custom task pane when the add-in starts, add the user control to the task pane in the Startup event handler of the add-in.

To display the custom task pane

  1. In Solution Explorer, expand PowerPoint.

  2. Right-click ThisAddIn.cs or ThisAddIn.vb and click View Code.

  3. Add the following code to the ThisAddIn class. This code declares instances of MyUserControl and CustomTaskPane as members of the ThisAddIn class.

    private MyUserControl myUserControl1;
    private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
  4. Replace the ThisAddIn_Startup event handler with the following code. This code creates a new CustomTaskPane by adding the MyUserControl object to the CustomTaskPanes collection. The code also displays the task pane.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
        myUserControl1 = new MyUserControl();
        myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1, "Calendar");
        myCustomTaskPane.Visible = true;

When you run the project, PowerPoint opens and the add-in displays the custom task pane. Click the MonthCalendar control to test the code.

To test your add-in

  1. Press F5 to run your project.

  2. Confirm that the custom task pane is visible.

  3. Click a date in the MonthCalendar control on the task pane.

    The date is inserted into the first slide in the active presentation.

You can learn more about how to create custom task panes from these topics:

Community Additions