How to get the Custom Task Pane Object on the Ribbon control Class

7.9k Views Asked by At

Developing a Excel vsto project, how could I handle the Custom Task Pane in the Class which is a Ribbon Control. For example, I would like to show the Custom Task Pane when I click the button of the Ribbon Control.

Dora

3

There are 3 best solutions below

0
On

This is a difficult challenge since the Ribbon and Task Panes are separate entities. One of the main challenges is that there is only one instance of the Ribbon class and multiple instances of the task pane for each inspector. To this properly requires some advanced understanding of the Office internals.

The solution also depends on whether you are using the Ribbon XML or Ribbon Designer. Which approach are you using?

0
On

I assume you are working with an Excel VSTO add-in, with the Ribbon Visual Designer. You can achieve what you want by making your custom Task Pane accessible via a property on your Add-In:

public partial class ThisAddIn
{
   private CustomTaskPane taskPane; 
   internal CustomTaskPane TaskPane
   {
      get
      {
         return this.taskPane;
      }
   }

... and adding a button in your Ribbon, and adding an event handler for the click event, accessing the add-in via Globals:

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

I wrote a post a while back which describes the process, you may find it useful. This is also feasible using the xml ribbon.

0
On

This can be accomplished by having a Win Forms user control. I've worked on a project where we had to extend MS Word and needed this functionality, but the same example will apply to Excel.

Another interesting way I stumbled upon on the net is to have a Windows user control and host a WPF user control within the Windows control! This ofcourse allows you to take advantage of all the awesome tools you get with WPF, here is an example:

1)Drop a ToggleButton on a Ribbon(Visual Designer).This will be used to show hide the task pane. Using ToggleButton is a good choice as it appears highlighted when pressed down.

2)Add below code to the click event of the ToggleButton

 Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;

3)Add a reference from your project to the following assembly - WindowsFormsIntegration

4)In your ThisAddIn.cs add the two using directives listed below:

   using Microsoft.Office.Tools;
   using System.Windows.Forms.Integration;

5)Add two user controls

5.1)User control (name - taskPaneControl1)

5.2)User control(WPF),(name - con)

Using the names I've used will help when copying/pasting the code below but by any means change it if you wish to

6)Add below code to the ThisAddIn.cs class

public CustomTaskPane TaskPane
{
    get{return taskPaneValue;}
}

private TaskPaneControl taskPaneControl1;
private CustomTaskPane taskPaneValue;
private WpfControl con;

internal void AddTaskPane()
{
    ElementHost host = new ElementHost();
    con = new WpfControl();
    host.Child = con;
    host.Dock = DockStyle.Fill;
    taskPaneControl1 = new TaskPaneControl();
    taskPaneControl1.Controls.Add(host);
    taskPaneValue = this.CustomTaskPanes.Add(taskPaneControl1, "My Taskpane");
    taskPaneValue.Visible = true;
}

6)Add the two code below to the Startup event in your ThisAddIn.cs

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddTaskPane();
taskPaneValue.Visible = false;
}

When an MS Office Application is opened the task pane will be hidden toggle the Visible property to change this in the Startup event. Navigate to the ToggleButton and press it a few times to make sure the task pane is showing as expected

Also have a look at the following link most of my code came from here - http://xamlcoder.com/cs/blogs/joe/archive/2007/07/17/using-wpf-with-vsto-office-2007.aspx