Freigeben über


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!

Comments

  • Anonymous
    April 04, 2007
    Hi,   It was good article to start custom task panes with. I am facing a problem in deploying the add-in.   I want to create a setup for my application, because I have to deploy it to different location. When I open word, after installing my add-in application, I can't see the custom task pane, as it is not activated. And if i try to activate it using manage COM Add-Ins, it is failing because, it shows error, failed to load dll. I even tried adding my dll path to trusted centers, but all in vain. What extra has to be done, in case of setups?

  • Anonymous
    April 06, 2007
    Hmmm... You need to trust the code at the location where it is installed. Check out the following documentation: http://msdn2.microsoft.com/en-us/library/k64zb6we(VS.80).aspx   And the following deployment article: http://go.microsoft.com/fwlink/?LinkID=57779 There is a custom action for trusting the customization. Here are a few more resources you can check out: http://blogs.msdn.com/mshneer/archive/2007/03/08/vsto-2005-se-deployment-paper-is-published.aspx http://blogs.msdn.com/mshneer/archive/2006/01/05/deployment-articles.aspx plus the VSTO Dev Center: http://msdn2.microsoft.com/en-us/office/aa905533.aspx

  • Anonymous
    July 01, 2007
     Dim ctp As Microsoft.Office.Tools.CustomTaskPane = Me.CustomTaskPanes.Add(ctrlTaskPane, "My Task Pane")        ctp.Visible = True Microsoft.Office.Tools.CustomTaskPane  for this am geeting exception

  • Anonymous
    July 02, 2007
    Hi iicdotnet, Perhaps try the following: Public ctp As CustomTaskPane = Nothing ctp = Me.CustomTaskPanes.Add(ctrlTaskPane, "My Task Pane") ctp.Visible = True A good place to validate your code would be on one of the How-To topics for creating a custom taks pane in VSTO where there are code snippets in both C# and VB.net. You can check it out here: http://msdn2.microsoft.com/en-us/library/bb508942.aspx. The entire How-To center can be found here: http://msdn2.microsoft.com/en-us/office/bb266408.aspx.

  • Anonymous
    July 04, 2007
    Is it Possible for creating Cuatomtack pane in for MSoffice 2003 ? only Action pane support for VSTO ?

  • Anonymous
    July 09, 2007
    Hi iicdotnet, Yes, you can definitely create custom task panes using VSTO for Office 2003. In VS, there is a node for Office 2003 and there you pick your project template and afterwards you'll add a user control. Hope this helps, Joanna

  • Anonymous
    July 11, 2007
    Hi Joanna, you said that it's possible to create a custom task pane in office 2003. I'm trying to create one in Word 2003 with VSTO and VS and i can't. Can you show me? please? Thanks, John

  • Anonymous
    July 16, 2007
    Hi John, Let me correct myself. When you create the ctp in VSTO SE for Office 2003, it's actually a document-level Actions Pane. Sorry for misleading you here. My mistake. This is a Word feature and not a VSTO feature. Here's some documentation on how to do this: http://blogs.msdn.com/kathleen/archive/2007/01/04/vsto-for-mere-mortals-video-series.aspx There's a few more in this list: http://blogs.msdn.com/kathleen/archive/2006/06/10/introductions.aspx If you have Kathleen McGrath's VSTO for Mere Mortal's book (which I highly suggest, she has a chapter on this as well. For documentation, you can find: http://msdn2.microsoft.com/en-us/library/7we49he1(VS.80).aspx http://msdn2.microsoft.com/en-us/library/kfzd656e(VS.80).aspx http://msdn2.microsoft.com/en-us/library/zbxt1ax4(VS.80).aspx http://msdn2.microsoft.com/en-us/library/h29yt5w6(VS.80).aspx

  • Anonymous
    September 04, 2007
    Hi Creating a custom task pane in office 2007 is seems very easy and there are lot of articles on it. Can you tell me how to Add Application level (not Workbook level)Custom Task pane in Excel 2003 Application. Thanks in Advance Binod

  • Anonymous
    September 05, 2007
    Unfortunately, creating an application level custom task pane in Word or Excel is not a supported feature in 2003. This is why you are can't find any resources on it. You can only create Actions Panes, which are document/workbook level.

  • Anonymous
    October 23, 2007
    Thanks for the great article. I have created an addin that works great when ran through VS. However if I try to install the files or go through Excel->addins-> COM addins it get an error saying that it is not a valid Office Add-in. Any help would be greatly appreciated. Thanks

  • Anonymous
    December 19, 2007
    Very nice , short but well detailed article.

  • Anonymous
    January 23, 2008
    Howdo boss.  Nice little blog you got going on here :) So I came across this because I've got a problem setting the visibility.  It works fine if I create my task pane and set Visible = true in ThisAddIn_Startup.   Problem comes when I try to toggle the visibility in response to an event.  In that case, calling MyTaskPaneInAPrivateVariableICreatedEarlier.Visible=false has no effect.  No exception either.  Any thoughts?

  • Anonymous
    July 03, 2008
    Hi everyBody can ou share the code for that task pane or Action pane in word 2003, using VSTO. is it possible? Actually i am new to this and i need to creatye a POC. Please help.

  • Anonymous
    July 03, 2008
    Hi guys i need the code at Application level. Thanks Regards Rahul

  • Anonymous
    November 27, 2009
    Many thanks for the simple and clear example of how to create a custom task pane.