Mathias Brandewinder on .NET, F#, VSTO and Excel development, and quantitative analysis / machine learning.
16. February 2010 19:27

Now that we have created the VSTO add-in project, it’s time to add some functionality to it. We want to provide a user interface to select what sheets we want to compare, and navigate between the differences that the add-in has found. In order to do this, we will create a custom task pane.

You can think of a custom task pane as a placeholder for controls. The best way to illustrate the concept is to simply do it. In our project, we will add a folder “TaskPane”, and add a new User Control by right-clicking on the TaskPane folder, which we will name “TaskPaneView”.

If you double-click on TaskPaneView, visual studio will display a gray empty area. This is the “canvas” on which we will add controls later, to allow the user to call the operations our add-in will expose. For now, we’ll leave it at that, and just focus on displaying the task pane.

Now go to the ThisAddIn class, and add the following code in the startup method:

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

Hit F5 to debut, and you should see the following:

Docked on the right-hand side of Excel, there is now an area title “Anakin” – that’s our task pane. The beauty of it is that it integrates smoothly with Excel, and gives you lots of flexibility. You can drag it to make it wider or narrower, depending on your real estate. More interestingly, you can move it around, and dock it to the left, top or bottom of the Excel window – or even undock it, and drag it anywhere you please, like this:

A quick comment on the code. The AddIn class exposes a CustomTaskPanesCollection, which is a collection of the task panes owned by the add-in (Captain Obvious strikes again). CustomTaskPanesCollection has an Add method, which requires a UserControl (the control displayed in the panel), and a title. Calling Add creates a new CustomTaskPane, and returns it. Like typical collections, the CustomTaskPanesCollection also allows removing of contents, by index or by passing it the pane that needs to be removed.

Note that adding a task pane is not sufficient. By default, a task pane is invisible, and needs to be made visible to appear.

Now we will want to access that task pane throughout the life of the add-in, so we need to provide a way to get to it. The most logical owner for the custom task pane is the add-in itself, so we will create a backing field for it, which will hold the reference, and a property to access it:

private CustomTaskPane taskPane;

{
get
{
}
}

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

Just to illustrate how one could add controls to the task pane, let’s build a quick-and-dirty example. Open the TaskPaneView user control again, and drag a button and a label on the surface, like this:

Right-click on the button and select Properties, go to the Text property in the Properties window, and change Text from Button1 to Click Me.

Similarly, change the label Text to an empty string. If you hit F5 right now, you’ll see that the Anakin Custom Task Pane now contains a button, which does nothing when you click it, in spite of requesting you to do so. Go back to the TaskPaneView, and double click the button. This will automatically create an event handler for the Button click event in TaskPaneView.cs, the code-behind file for the control. Let’s add the following code:

private void button1_Click(object sender, System.EventArgs e)
{
var time = DateTime.Now.ToLongTimeString();
this.label1.Text = time;
}

Now when you click the button, the label will display the time at which it was clicked. Not the most impressive peace of functionality, but hey, it proves the point.

That’s it for today! Next time, we will address a problem. Our Custom Task Pane shows up when we fire Excel, but if you close it, it’s gone, and right now there is no way to make it come back. We’ll use the Ribbon to resolve that issue.

Questions, comments? Please let me know, I’d love to make this series as useful as possible!

2/12/2010 10:23:33 AM #

Create an Excel 2007 VSTO add-in: the Ribbon

Create an Excel 2007 VSTO add-in: the Ribbon

7/26/2010 3:08:44 PM #

Hi, I'm following this example. I think it's really useful for beginers, but I have a problem If i write on VS lines 01 and 03 ( "private CustomTaskPane taskPane;" and "private CustomTaskPane TaskPane") i have problems when debuging, instead of that I have to write "private Microsoft.Office.Tools.CustomTaskPane taskPane;" and "private Microsoft.Office.Tools.CustomTaskPane TaskPane" to solve problems. ¿Why does it occur? Thanks.

René

7/26/2010 4:02:24 PM #

Hi Rene,
Thanks for the question! You are right, there is a step here which is not clear. As you noticed, the class CustomTaskPane exists in the Microsoft.Office.Tools namespace, and when you create the add-in project, in the top section (where all the using... statements are), it is not listed. If you add a line there:
using Microsoft.Office.Tools;
then the problem should disappear: all the classes that are referenced in Microsoft.Office.Tools now become available to you. Your solution works, too: you are being explicit about where that class is coming from.
One Visual Studio trick can help you there. If you type in private CustomTaskPane, CustomTaskPane should show up in red, because the reference is missing. If you click on it, you should see a little red dot at the end of the name, slightly under it. If you move your mouse over, you'll see a little drop-drown menu, and the first option should be "using Microsoft.Office.Tools". If you click that, Visual Studio should automatically add the reference in the using statements!
Thanks again for the catch,
Mathias

9/27/2010 2:16:45 PM #

Hello Mathias,,

I am trying to add a custom pane . But i do not have this.CustomPane in my ThisAddin class.
I am not able to obtain the CustomPane. I am using VS2008 Professional Edition.
IS ther e something wrong?  Is it not available in Professional edition?

Pls let me know. .Thanks in advance. sandeep

9/27/2010 2:17:50 PM #

I have the namespace Microsoft.Office.Tools, but in this too there s no CustomPane.

Thanks
Sandeep

9/27/2010 2:19:04 PM #

Thanks
Sandeep

9/27/2010 2:53:05 PM #

9/27/2010 3:59:12 PM #

Hi Sandeep,
This is odd. I actually wrote the add-in using VS2008 Professional, so this shouldn't be an issue. Is your project a VSTO Excel 2007 add-in? Task panes are not supported for Excel 2003, but for 2007 it should work just fine. Are you sure you didn't make a typing mistake in this.CustomTaskPanes ?
Mathias

10/5/2010 1:53:27 PM #

Hello I want to develop an addin for Excel 2003,
Is there any way to develop addin s for Excel 2003.
Pls help
Thanks
Sandeep

10/5/2010 2:27:22 PM #

Hi Sandeep,
Yes you can develop add-ins for Excel 2003 using VSTO, but it is a bit more difficult than for Excel 2007 or 2010. First, you have to use Visual Studio 2008, VS2010 doesn't support that type of project any more. Then, you cannot use the Ribbon, for obvious reasons, or Custom Task panes - and a few other features are unavailable to you. Finally, the deployment is also a bit more complex.
Hope this helps!
Mathias

12/20/2010 4:29:15 PM #

Create an Excel 2007 VSTO add-in: display open worksheets in a TreeView

Create an Excel 2007 VSTO add-in: display open worksheets in a TreeView

10/2/2011 6:40:07 AM #

I have a question, is it possible to add a customtaskpane to a excel workbook project ?? Thanks

Rene

10/2/2011 1:55:23 PM #

Thanks for taking the time to write this up.

12/21/2011 3:23:53 AM #

Thank you!

3/20/2012 9:42:51 PM #

Thanks for add that kind of page.