Playing with Visual Studio Tools for Office Today and wanted to do something simple like like show a customtaskpane from a button click in a Ribbon.

The challenge arises from the fact the customtask pane needs to exist off of the parent Addin object that hosts the application...Andrew Coates solves this in an elegant way where he creates the customtaskpane in the load of the ThisAddIn and makes the customtaskpane a global member and toggles its visibility from the ribbon...But being far lazier than Andrew when i found half this work is already done for you via the Globals module i decided to use different method.  

 

So the steps to do this in Visual Basic 2008:

 

  1. File new > Project > Office > 2007 (make sure you have Framework 3.5 chosen!) > Excel Addin
  2. Project Menu > Add new item >Ribbon (Visual Designer)
  3. Project Menu > Add New Item > UserControl
  4. Add a button to the Ribbon and double click to bring up the default event handler
  5. Add the following code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click

    Dim cu As New UserControl1
    Dim ctp As Microsoft.Office.Tools.CustomTaskPane
    ctp = Globals.ThisAddIn.CustomTaskPanes.Add(cu, "Hello")
    ctp.Visible = True

    End Sub

Of course EVERY time you run this code you get a new task pane probably best to create the customtaskpane only once and then toggle the visibility

 

Public Class Ribbon1


Dim ctp As Microsoft.Office.Tools.CustomTaskPane

Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load


Dim cu As New UserControl1
ctp = Globals.ThisAddIn.CustomTaskPanes.Add(cu, "Hello")


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click

ctp.Visible = Not ctp.Visible

End Sub

End Class