Creating Custom Task Panes Using VSTO SE – A Beginner’s Guide

In order to create Office Business Applications (OBAs), you need to understand the basics. There are a lot of blogs out there which talk about one set of features, one set of technologies, or one product. In my blog, I want to be able to cover a wide breadth of Office technologies and products so that you can understand the basics and then I can show you how to stitch all that together so that you can start building OBAs.

Before I move on with today's topic, in the webcast I did last week on Customizing the Office UI, someone ask an excellent question on whether there are any resources on customizing the ribbon with C++. Jensen Harris from the Office UX team just blogged on this a few days ago so all you C++ devs may want to check out what he has to say.

In my last post I showed you how to customize the ribbon in Excel using Visual Studio 2005 Tools for the 2007 Microsoft Office System Second Edition (VSTO SE) . So how would you create a basic custom task pane using the same tool? Let's match that ribbon up with a custom task pane in Excel, stepping through it in a step-by step manner:

STEP 1: Download VSTO SE

STEP 2: Create a new project

Startup Visual Studio 2005 and create a new project. Under your language preference node (C#, VB.NET, etc. – I'm using C#), click on the "Office" node, then the "2007 Add-ins" node, then "Excel Add-in" and click OK.

Like I mentioned in my previous post, VSTO SE gives you all the templates under the Office node so you not only get the ones for Office 2007 but for Office 2003 as well. This means that your solutions built on previous versions will run just fine if you are using VSTO SE.

STEP 3: Add a user control which will be your custom task pane

The first thing you will see is that within ThisAddIn.cs, you have two methods: ThisAddIn_Startup and ThisAddIn_Shutdown. These methods actually wrap up a lot of the methods that you would have to manually write yourself in Visual Studio 2005 sans VSTO SE. They are awesome wrapper classes that save you a lot of time. You can just get right down to work. You'll also notice that all of the appropriate references are there, no need to add the basics.

Right-click on the project file, go to add > New Item… and then click on User Control. Click ok and as you do, you will see that UserControl1.cs is added along with a form which you can add controls to from the toolbox. Behind the scenes, VSTO SE has done the work to implement ICustomTaskPaneConsumer, to make the user control COM visible so that it becomes an activeX control which can be used within the Office application. A lot less plumbing work, essentially none.

STEP 4: Replace the StartUp method of ThisAddIn class

Here's the code...first create an instance of the user control and then replace the StartUp method.

private UserControl1 ctrlTaskPane;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

ctrlTaskPane = new UserControl1();

Microsoft.Office.Tools.CustomTaskPane ctp =

this.CustomTaskPanes.Add(ctrlTaskPane, "My Task Pane");

ctp.Visible = true;

}

What we are doing here is first instantiating the user control, adding our user control to the collection of Office custom task panes and setting the title to be "My Task Pane" and finally, making the task pane visible. The latter is required since by default the task pane is set to visible=false.

STEP 5: Ready, set, BUILD!

Build your solution and in the Solution pane under the setup project, install it. Go through the wizard, click F5 and we're done. Excel opens with a plain task pane, empty of controls (since we didn't both adding any) entitled "My Task Pane". Incredibly easy.

As I mentioned in my last post, the reason of this simplicity in programmability isn't to not right any code, the point is to write the plumbing code for you, so you won't have to. This gives developers the power and the time back to go be their productive selves and newer developers the ability to learn with less frustration.

Happy coding & experimenting!