Mathias Brandewinder on .NET, VSTO and Excel development, and quantitative analysis.
by Mathias 22. February 2010 10:23

In our previous installment, we went through adding a Custom Task Pane to Excel to host the user interface of our VSTO add-in. However, we left off with one problem to solve. The task pane is shown when the add-in starts up, but if the user closes it, there is no mechanism to show it again. We will resolve that problem by using the ribbon, adding a button that restores the task pane visibility.

First, we will create a new folder in our project, called “Ribbon”. Right-click the folder, select “Add > New Item”, and pick “Ribbon (Visual Designer)” from the available templates. We will call our ribbon “AnakinRibbon”.

By now, your solution should look like this:

RibbonFolder

Visual Studio displays a visual interface, representing the ribbon we will use for Anakin:

EmptyRibbon

By default, the ribbon comes pre-populated with a tab called “TabAddIns”, labeled Built-In. This reflects the fact that, by default, your add-in ribbon will show up in the standard Add-Ins tab of the ribbon.

While this would be perfectly acceptable, we actually want to add our add-in to an existing Ribbon tab, the “Review” tab. It seems like a natural place to find functionality related to comparing different versions of a spreadsheet, and this way, we can avoid crowding the Ribbon with new tabs, and integrate seamlessly with Office, without minimal disturbance to the user experience.

First, we will add a new Tab to our Ribbon. Expanding the Office Ribbon Controls section in the Toolbox reveals the set of controls we can use, most of them familiar.

AvailableRibbonControls

Grab the Tab control, and drag it over the Ribbon (or right-click the Ribbon, and select Add Ribbon Tab). A new tab shows up, which is not marked as built-in. If you hit F5 at that point, you will see two things: the Add-Ins tab now contains an empty group (the group that is created by default), and a new tab has been created for us, where we could add controls if we wanted to use a custom tab.

ExtraTab

First let’s hook up the new tab, so that its contents show up in the Review tab, instead of inside a new one. To do this, we need to provide the Tab with the Id of the built-in Excel tab it will be hosted in. Select the tab, and in the Properties window, modify the ControlIdType from “Custom” to “Office”, to indicate you want to use a built-in office tab, and type in “TabReview” in the OfficeId field.

 BuiltInTab

Once this is done, the display of the Tab will change to TabReview (Built-In), indicating that the Ribbon recognized what we wanted to do. Each control built in the Ribbon has an Id, which you need to use if you want to access it – the complete list, for all Office applications, can be found here.

UsingBuiltInRibbonTab

Next, let’s create a Group for our add-in, change its Label to Anakin, and drag a Button to the group, which we will label “Compare”.

RibbonWithButton

Hit F5 to debut the project, you should see something like this, with the Review tab now containing our group and button.

AddInWithButton Now let’s add some code, so that when the button is clicked, the Custom Task Pane visibility is set to true. First, we need to make the TaskPane property accessible, so we change the corresponding property on the ThisAddIn class from private to internal:

public partial class ThisAddIn
{
    private CustomTaskPane taskPane;

    internal CustomTaskPane TaskPane
    {
        get
        {
            return this.taskPane;
        }
    }

On the Ribbon, double-click on the button. An empty event handler is created for you – let’s add the following code to it:

private void ShowAnakin_Click(object sender, RibbonControlEventArgs e)
{
    Globals.ThisAddIn.TaskPane.Visible = true;
}

The Globals class exposes an internal static property ThisAddin, which provides access to the Add-In from anywhere within the add-in solution. We use it to navigate to the TaskPane, and make it visible whenever the button is clicked.

Let’s make a small modification, so that by default the task pane is hidden:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    var taskPaneView = new TaskPaneView();
    this.taskPane = this.CustomTaskPanes.Add(taskPaneView, "Anakin");
    this.taskPane.Visible = false;
}

The last thing we have to do is to remove the default add-in tab and its group from the ribbon. Open the AnakinRibbon designer, right-click on the AddInTab area, and delete – and we are done.

Now that our hooks are in place, we can begin to add some real functionality to our add-in. In the next installment, we will work on adding a tree view to the Custom Task Pane, so that the user can select which of the currently open worksheets he/she wants to run a comparison against. We will use WPF for that control – because the WPF tree view control is great, and because the ability to use WPF in Office Applications is a fantastic feature!

Resources

List of the built-in ribbon tabs

Comments

2/27/2010 1:21:45 AM #

trackback

Create an Excel 2007 VSTO add-in: adding a WPF control

Create an Excel 2007 VSTO add-in: adding a WPF control

Clear Lines Blog | Reply

5/3/2010 5:22:05 PM #

Rheology modifiers

I do like your theme here. Great and excellent sources of ideas and information. Please keep on sharing!

Rheology modifiers United States | Reply

11/26/2010 3:15:23 AM #

patricio cuaron

I'd change the button proc to this:
lobals.ThisAddIn.CambiosTaskPane.Visible = Not Globals.ThisAddIn.CambiosTaskPane.Visible

patricio cuaron Argentina | Reply

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

trackback

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

Clear Lines Blog | Reply

5/10/2011 7:55:46 AM #

Fane

When I press the button the debugger stops and makes yellow the line:
        Globals.ThisAddIn.TaskPane.Visible = true;
and the error "NullReferenceException was unhandled by user code"
                           'Object reference not set to an instance of an object'
I changed the line from private in 'internal CustomTaskPane TaskPane'
What I did wrong?

Fane Romania | Reply

5/10/2011 8:05:20 AM #

Fane

On the code line: 'private CustomTaskPane taskPane;'
the word 'taskPane' is green underlined and the next message is displayed when the mouse is on it:
  "Field ExcelAddin.ThisAddin_C.TaskPane ' is never assigned to, and will always have its default value null".
This line of code is inside thisAddin class like this:
namespace ExcelAddIn_C
{
    public partial class ThisAddIn
    {
        private CustomTaskPane taskPane;

        internal CustomTaskPane TaskPane
        {
            get
            {
                return this.taskPane;
            }
        }
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            var messageBox = MessageBox.Show("Salutari din Comm Addin (C#)!");
            var taskPaneView = new TaskPaneView();
            var myTaskPane = this.CustomTaskPanes.Add(taskPaneView, "Fane Duru");
            myTaskPane.Visible = true;
        }

Fane Romania | Reply

5/15/2011 3:07:37 AM #

mathias

From the code you attached, it seems that the following is happening: in ThisAddIn_Startup, a TaskPane is created as a local variable myTaskPane, but that variable is never assigned to the field taskPane. Adding the line this.taskPane = myTaskPane at the end of the ThisAddIn_Startup should address that issue.
Mathias

mathias United States | Reply

12/17/2011 8:03:46 PM #

Faisal

how can i give selection event on a ribbon tab? any one please help me...

Faisal Bangladesh | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Comments

Comment RSS