Walkthrough: Automate an application from a custom task pane
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
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 VSTO Add-in projects for Outlook. 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.
Note
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 Personalize the Visual Studio IDE.
Prerequisites
You need the following components to complete this walkthrough:
An edition of Visual Studio that includes the Microsoft Office developer tools. For more information, see Configure a computer to develop Office solutions.
Microsoft PowerPoint 2010 or PowerPoint 2013.
Create the Add-in project
The first step is to create a VSTO Add-in project for PowerPoint.
To create a new project
Create a PowerPoint VSTO 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.
Design the user interface of the custom task pane
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
On the Project menu, click Add User Control.
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.
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.
Automate PowerPoint from the custom task pane
The purpose of the VSTO 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
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.
Add the following code to the top of the file. This code creates aliases for the Microsoft.Office.Core and PowerPoint namespaces.
using Office = Microsoft.Office.Core; using PowerPoint = Microsoft.Office.Interop.PowerPoint;
Imports Office = Microsoft.Office.Core Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
Add the following code to the
MyUserControl
class. This code declares a Shape object as a member ofMyUserControl
. 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;
Private TextBox As PowerPoint.Shape
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 theGlobals.ThisAddIn
object to access the object model of PowerPoint.private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e) { try { if (textbox != null) { textbox.Delete(); } PowerPoint.Slide slide = Globals.ThisAddIn.Application.ActivePresentation.Slides[1]; textbox = slide.Shapes.AddTextbox( Office.MsoTextOrientation.msoTextOrientationHorizontal, 50, 100, 600, 50); textbox.TextFrame.TextRange.Text = e.Start.ToLongDateString(); textbox.TextFrame.TextRange.Font.Size = 48; textbox.TextFrame.TextRange.Font.Color.RGB = Color.DarkViolet.ToArgb(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
Private Sub MonthCalendar1_DateChanged( _ ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DateRangeEventArgs) _ Handles MonthCalendar1.DateChanged Try If TextBox IsNot Nothing Then TextBox.Delete() End If Dim Slide As PowerPoint.Slide Slide = Globals.ThisAddIn.Application.ActivePresentation.Slides(1) TextBox = Slide.Shapes.AddTextbox( _ Office.MsoTextOrientation.msoTextOrientationHorizontal, _ 50, 100, 600, 50) TextBox.TextFrame.TextRange.Text = e.Start.ToLongDateString() TextBox.TextFrame.TextRange.Font.Size = 48 TextBox.TextFrame.TextRange.Font.Color.RGB = _ System.Drawing.Color.DarkViolet.ToArgb() Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString()) End Try End Sub
In Solution Explorer, right-click the MyAddIn project and then click Build. Verify that the project builds without errors.
Display the custom task pane
To display the custom task pane when the VSTO Add-in starts, add the user control to the task pane in the Startup event handler of the VSTO Add-in.
To display the custom task pane
In Solution Explorer, expand PowerPoint.
Right-click ThisAddIn.cs or ThisAddIn.vb and click View Code.
Add the following code to the
ThisAddIn
class. This code declares instances ofMyUserControl
and CustomTaskPane as members of theThisAddIn
class.Private myUserControl1 As MyUserControl Private myCustomTaskPane As Microsoft.Office.Tools.CustomTaskPane
private MyUserControl myUserControl1; private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
Replace the
ThisAddIn_Startup
event handler with the following code. This code creates a new CustomTaskPane by adding theMyUserControl
object to theCustomTaskPanes
collection. The code also displays the task pane.Private Sub ThisAddIn_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup myUserControl1 = New MyUserControl myCustomTaskPane = Me.CustomTaskPanes.Add(myUserControl1, "Calendar") myCustomTaskPane.Visible = True End Sub
private void ThisAddIn_Startup(object sender, System.EventArgs e) { myUserControl1 = new MyUserControl(); myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1, "Calendar"); myCustomTaskPane.Visible = true; }
Test the Add-in
When you run the project, PowerPoint opens and the VSTO Add-in displays the custom task pane. Click the MonthCalendar control to test the code.
To test your VSTO Add-in
Press F5 to run your project.
Confirm that the custom task pane is visible.
Click a date in the MonthCalendar control on the task pane.
The date is inserted into the first slide in the active presentation.
Next steps
You can learn more about how to create custom task panes from these topics:
Create a custom task pane in a VSTO Add-in for a different application. For more information about the applications that support custom task panes, see Custom task panes.
Create a Ribbon button that can be used to hide or display a custom task pane. For more information, see Walkthrough: Synchronize a custom task pane with a Ribbon button.
Create a custom task pane for every e-mail message that is opened in Outlook. For more information, see Walkthrough: Display custom task panes with email messages in Outlook.