VSTO: My Favorite Feature - Custom Task Panes
My avid readers (both of you) know that I've done quite a bit of work around Outlook 2007 Form Regions in Visual Studio 2005 Tools for Office. If you missed those posts, you can read more about them here and here. Form Regions are a great way to add custom functionality to Outlook forms but there's another way you can add features and Windows forms to all of the Office applications and unlike Form Regions (pre-Orcas) it's really easy. Therefore one of my favorite features is Custom Task Panes.
Adding a custom task pane couldn't be much easier. Just add a UserControl to your Add-in project and build any functionality you'd like inside that control. You can use the databinding features of WinForms, third party controls, and even COM+ components. Then to use that UserControl as a custom task pane just add it to the CustomTaskPaneCollection like below.
Dim ctp As Microsoft.Office.Tools.CustomTaskPane = Me.CustomTaskPanes.Add(New MyUserControl(), "Product List")
You can then make the task pane visible either at startup or when the user clicks a button on the Ribbon. For a great example of how to properly implement a custom task pane's visibility check out Ken Getz' MSDN Webcast.
By default a custom task pane is going to appear docked on the right side of the window for your application. You can however specify where you want the custom task pane to display by using the DockPosition parameter.
ctp.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionBottom
The possible options for the DockPosition are:
- MsoCTPDockPosition.msoCTPDockPositionBottom
- MsoCTPDockPosition.msoCTPDockPositionFloating
- MsoCTPDockPosition.msoCTPDockPositionLeft
- MsoCTPDockPosition.msoCTPDockPositionRight
- MsoCTPDockPosition.msoCTPDockPositionTop
You can also respond to the user changing the task pane's position by using the DockPositionChanged event.
Private Sub CTP_DockPositionChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ctp As Microsoft.Office.Tools.CustomTaskPane = CType(sender, Microsoft.Office.Tools.CustomTaskPane)
'Do something meaningful here
End Sub
Lastly, you can restrict where the user can dock your task pane using the DockPositionRestrict property. The possible options are:
- msoCTPDockPositionRestrictNoChange
- msoCTPDockPositionRestrictNoHorizontal
- msoCTPDockPositionRestrictNone
- msoCTPDockPositionRestrictNoVertical
And that ladies and gents is all there is to that.