Ever since the task pane was introduced in Microsoft Office XP for the first time, Office solution developers have asked, "How can I create my own version of a task pane to use as part of my Office solutions?" (If you're not sure what the task pane is, click Task Pane on the View menu in an application such as the XP or 2003 versions of Microsoft Office Excel or Microsoft Office Word.)
Starting with Microsoft Office 2003, you can create your own version of the task pane. More specifically, you can enable your end users and Excel workbooks or Word documents to interact with the built-in Document Actions task pane, whose layout you can modify. We call this type of Office solution a smart document solution. (It’s true that there are other types of smart document solutions that don't rely on customized task panes, but the task pane is used in almost all smart document solutions out there.)
Microsoft Visual Studio Tools for the Microsoft Office System, Version 2.0 extends and improves the smart document solution development infrastructure for Excel and Word. We use the term actions pane to differentiate the a Visual Studio Tools for Office, Version 2.0 solution that uses the Document Actions task pane from its built-in Office counterpart, a smart document solution. How does the actions pane extend and improve on smart documents?
Here's how easy it is to create an actions pane in a Visual Studio Tools for Office, Version 2.0 project.
First, create an Excel Application, choosing either the Visual Basic or C# language. After the project is created, create two .NET UserControls and keep their default control names, UserControl1 and UserControl2. (To create a UserControl and add it to the current project, on the Project menu, click Add UserControl, and click Add.) To ensure that you can see the UserControls when you run the project, you may want to add a label control, a text box control, and a button control to each user control with some default display text.
Next, in the ThisWorkbook code module, create an instance of each UserControl just prior to the ThisWorkbook_Initialize event.
' Visual Basic Dim uc1 As UserControl1 Dim uc2 As UserControl2 // C# UserControl1 uc1; UserControl2 uc2;
Next, initialize the UserControls inside of the ThisWorkbook_Initialize event.
' Visual Basic uc1 = New UserControl1() uc2 = New UserControl2() // C# uc1 = new UserControl1(); uc2 = new UserControl2();
Next, display each UserControl on the actions pane inside of the ThisWorkbook_Initialize event.
' Visual Basic Me.ActionsPane.Controls.Add(uc1) Me.ActionsPane.Controls.Add(uc2) // C# this.ActionsPane.Controls.Add(uc1); this.ActionsPane.Controls.Add(uc2);
Whenever you want to hide a UserControl (for example, when an end user moves focus to a different named range), call the Remove method.
' Visual Basic Me.ActionsPane.Controls.Remove(uc1) // C# this.ActionsPane.Controls.Remove(uc1);
Similarly, whenever you want to show a UserControl, call the Add method again.
' Visual Basic Me.ActionsPane.Controls.Add(uc1) // C# this.ActionsPane.Controls.Add(uc1);
And that's it!
In future blog posts, I will show you how to build upon this simple example by writing code that allows UserControls on the actions pane to interact with the document, and vice versa.
-- Paul Cornell
-----This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm.