Walkthrough: Displaying Custom Task Panes with E-Mail Messages in Outlook
This walkthrough demonstrates how to display a unique instance of a custom task pane with each e-mail message that is created or opened. Users can display or hide the custom task pane by using a button on the Ribbon of each e-mail message.
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.
To display a custom task pane with multiple Explorer or Inspector windows, you must create an instance of the custom task pane for every window that is opened. For more information about the behavior of custom task panes in Outlook windows, see Custom Task Panes.
|
This walkthrough illustrates the following tasks:
Designing the user interface (UI) of the custom task pane.
Creating a custom Ribbon UI.
Displaying the custom Ribbon UI with e-mail messages.
Creating a class to manage Inspector windows and custom task panes.
Initializing and cleaning up resources used by the VSTO Add-in.
Synchronizing the Ribbon toggle button with the custom task pane.
|
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 Configuring a Computer to Develop Office Solutions.
Microsoft Outlook 2013 or Microsoft Outlook 2010.
For a related video demonstration, see How Do I: Use Task Panes in Outlook?.
Custom task panes are implemented in VSTO Add-ins. Start by creating an VSTO Add-in project for Outlook.
To create a new project
Create an Outlook Add-in project with the name OutlookMailItemTaskPane. Use the Outlook 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 OutlookMailItemTaskPane project to Solution Explorer.
There is no visual designer for custom task panes, but you can design a user control with the UI you want. The custom task pane in this VSTO Add-in has a simple UI that contains a TextBox control. 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
In Solution Explorer, click the OutlookMailItemTaskPane project.
On the Project menu, click Add User Control.
In the Add New Item dialog box, change the name of the user control to TaskPaneControl, and then click Add.
The user control opens in the designer.
From the Common Controls tab of the Toolbox, drag a TextBox control to the user control.
One of the goals for this VSTO Add-in is to give users a way to hide or display the custom task pane from the Ribbon of each e-mail message. To provide the user interface, create a custom Ribbon UI that displays a toggle button that users can click to display or hide the custom task pane.
To create a custom Ribbon UI
On the Project menu, click Add New Item.
In the Add New Item dialog box, select Ribbon (Visual Designer).
Change the name of the new Ribbon to ManageTaskPaneRibbon, and click Add.
The ManageTaskPaneRibbon.cs or ManageTaskPaneRibbon.vb file opens in the Ribbon Designer and displays a default tab and group.
In the Ribbon Designer, click group1.
In the Properties window, set the Label property to Task Pane Manager.
From the Office Ribbon Controls tab of the Toolbox, drag a ToggleButton control onto the Task Pane Manager group.
Click toggleButton1.
In the Properties window, set the Label property to Show Task Pane.
The custom task pane that you create in this walkthrough is designed to appear only with Inspector windows that contain e-mail messages. Therefore, set the properties to display your custom Ribbon UI only with these windows.
To display the custom Ribbon UI with e-mail messages
In the Ribbon Designer, click the ManageTaskPaneRibbon Ribbon.
In the Properties window, click the drop-down list next to RibbonType, and select Microsoft.Outlook.Mail.Compose and Microsoft.Outlook.Mail.Read.
There are several cases in which the VSTO Add-in must identify which custom task pane is associated with a specific e-mail message. These cases include the following:
When the user closes an e-mail message. In this case, the VSTO Add-in must remove the corresponding custom task pane to ensure that resources used by the VSTO Add-in are cleaned up correctly.
When the user closes the custom task pane. In this case, the VSTO Add-in must update the state of the toggle button on the Ribbon of the e-mail message.
When the user clicks the toggle button on the Ribbon. In this case, the VSTO Add-in must hide or display the corresponding task pane.
To enable the VSTO Add-in to keep track of which custom task pane is associated with each open e-mail message, create a custom class that wraps pairs of T:Microsoft.Office.Interop.Outlook.Inspector and CustomTaskPane objects. This class creates a new custom task pane object for each e-mail message, and it deletes the custom task pane when the corresponding e-mail message is closed.
To create a class to manage Inspector windows and custom task panes
In Solution Explorer, right-click the ThisAddIn.cs or ThisAddIn.vb file, and then click View Code.
Add the following statements to the top of the file.
Add the following code to the ThisAddIn.cs or ThisAddIn.vb file, outside the
ThisAddInclass (for Visual C#, add this code inside theOutlookMailItemTaskPanenamespace). TheInspectorWrapperclass manages a pair of T:Microsoft.Office.Interop.Outlook.Inspector and CustomTaskPane objects. You will complete the definition of this class in the following steps.Add the following constructor after the code that you added in the previous step. This constructor creates and initializes a new custom task pane that is associated with the T:Microsoft.Office.Interop.Outlook.Inspector object that is passed in. In C#, the constructor also attaches event handlers to the E:Microsoft.Office.Interop.Outlook.InspectorEvents_Event.Close event of the T:Microsoft.Office.Interop.Outlook.Inspector object and to the VisibleChanged event of the CustomTaskPane object.
Add the following method after the code that you added in the previous step. This method is an event handler for the VisibleChanged event of the CustomTaskPane object that is contained in the
InspectorWrapperclass. This code updates the state of the toggle button whenever the user opens or closes the custom task pane.Add the following method after the code that you added in the previous step. This method is an event handler for the E:Microsoft.Office.Interop.Outlook.InspectorEvents_Event.Close event of the T:Microsoft.Office.Interop.Outlook.Inspector object that contains the current e-mail message. The event handler frees resources when the e-mail message is closed. The event handler also removes the current custom task pane from the
CustomTaskPanescollection. This helps prevent multiple instances of the custom task pane when the next e-mail message is opened.Add the following code after the code that you added in the previous step. Later in this walkthrough, you will call this property from a method in the custom Ribbon UI to display or hide the custom task pane.
Add code to the ThisAddIn class to initialize the VSTO Add-in when it is loaded, and to clean up resources used by the VSTO Add-in when it is unloaded. You initialize the VSTO Add-in by setting up an event handler for the E:Microsoft.Office.Interop.Outlook.InspectorsEvents_Event.NewInspector event and by passing all existing e-mail messages to this event handler. When the VSTO Add-in is unloaded, detach the event handler and clean up objects used by the VSTO Add-in.
To initialize and clean up resources used by the VSTO Add-in
In the ThisAddIn.cs or ThisAddIn.vb file, locate the definition of the
ThisAddInclass.Add the following declarations to the
ThisAddInclass:The
inspectorWrappersValuefield contains all the T:Microsoft.Office.Interop.Outlook.Inspector andInspectorWrapperobjects that are managed by the VSTO Add-in.The
inspectorsfield maintains a reference to the collection of Inspector windows in the current Outlook instance. This reference prevents the garbage collector from freeing the memory that contains the event handler for the E:Microsoft.Office.Interop.Outlook.InspectorsEvents_Event.NewInspector event, which you will declare in the next step.
Replace the
ThisAddIn_Startupmethod with the following code. This code attaches an event handler to the E:Microsoft.Office.Interop.Outlook.InspectorsEvents_Event.NewInspector event, and it passes every existing T:Microsoft.Office.Interop.Outlook.Inspector object to the event handler. If the user loads the VSTO Add-in after Outlook is already running, the VSTO Add-in uses this information to create custom task panes for all e-mail messages that are already open.Replace the
ThisAddIn_ShutDownmethod with the following code. This code detaches the E:Microsoft.Office.Interop.Outlook.InspectorsEvents_Event.NewInspector event handler and cleans up objects used by the VSTO Add-in.Add the following E:Microsoft.Office.Interop.Outlook.InspectorsEvents_Event.NewInspector event handler to the
ThisAddInclass. If a new T:Microsoft.Office.Interop.Outlook.Inspector contains an e-mail message, the method creates an instance of a newInspectorWrapperobject to manage the relationship between the e-mail message and the corresponding task pane.Add the following property to the
ThisAddInclass. This property exposes the privateinspectorWrappersValuefield to code outside theThisAddInclass.
Build your project to ensure that it compiles without errors.
To build your project
- In Solution Explorer, right-click the OutlookMailItemTaskPane project and then click Build. Verify that the project compiles without errors.
The toggle button will appear to be pressed in when the task pane is visible, and it will appear to be not pressed in when the task pane is hidden. To synchronize the state of the button with the custom task pane, modify the Click event handler of the toggle button.
To synchronize the custom task pane with the toggle button
In the Ribbon Designer, double-click the Show Task Pane toggle button.
Visual Studio automatically generates an event handler named
toggleButton1_Click, which handles the Click event of the toggle button. Visual Studio also opens the ManageTaskPaneRibbon.cs or ManageTaskPaneRibbon.vb file in the Code Editor.Add the following statements to the top of the ManageTaskPaneRibbon.cs or ManageTaskPaneRibbon.vb file.
Replace the
toggleButton1_Clickevent handler with the following code. When the user clicks the toggle button, this method hides or displays the custom task pane that is associated with the current Inspector window.
When you start debugging the project, Outlook opens and the VSTO Add-in is loaded. The VSTO Add-in displays a unique instance of the custom task pane with each e-mail message that is opened. Create several new e-mail messages to test the code.
To test the VSTO Add-in
Press F5.
In Outlook, click New to create a new e-mail message.
On the Ribbon of the e-mail message, click the Add-Ins tab, and then click the Show Task Pane button.
Verify that a task pane with the title My task pane is displayed with the e-mail message.
In the task pane, type First task pane in the text box.
Close the task pane.
Verify that the state of the Show Task Pane button changes so that it is no longer pressed.
Click the Show Task Pane button again.
Verify that the task pane opens, and that the text box still contains the string First task pane.
In Outlook, click New to create a second e-mail message.
On the Ribbon of the e-mail message, click the Add-Ins tab, and then click the Show Task Pane button.
Verify that a task pane with the title My task pane is displayed with the e-mail message, and the text box in this task pane is empty.
In the task pane, type Second task pane in the text box.
Change focus to the first e-mail message.
Verify that the task pane that is associated with this e-mail message still displays First task pane in the text box.
This VSTO Add-in also handles more advanced scenarios that you can try. For example, you can test the behavior when viewing e-mails by using the Next Item and Previous Item buttons. You can also test the behavior when you unload the VSTO Add-in, open several e-mail messages, and then reload the VSTO Add-in.
You can learn more about how to create custom task panes from these topics:
Create a custom task pane in an VSTO Add-in for a different application. For more information about the applications that support custom task panes, see Custom Task Panes.
Automate a Microsoft Office application by using a custom task pane. For more information, see Walkthrough: Automating an Application from a Custom Task Pane.
Create a Ribbon button in Excel that can be used to hide or display a custom task pane. For more information, see Walkthrough: Synchronizing a Custom Task Pane with a Ribbon Button.
Custom Task Panes
How to: Add a Custom Task Pane to an Application
Walkthrough: Automating an Application from a Custom Task Pane
Walkthrough: Synchronizing a Custom Task Pane with a Ribbon Button
Ribbon Overview
Outlook Object Model Overview
Accessing the Ribbon at Run Time