Jerry Nixon @Work: Control custom task panes (CustomTaskPane) from a custom Office 2007 fluent ribbon - with Visual Studio 2008 b2

Jerry Nixon on Windows

Wednesday, August 8, 2007

Control custom task panes (CustomTaskPane) from a custom Office 2007 fluent ribbon - with Visual Studio 2008 b2

This is based on Visual Studio 2008 Beta 2 and Microsoft Office 2007.

This demo is special because I use a WPF user control.

Step 1: Create a Custom Controls (WPF) type project (call it CustomControls)

Create a user control (call it UserControl1) and add a button to it (or any other control type you want).

Step 2: Create an Office 2007 Excel AddIn project (call it ExcelMainAddIn)

Add a reference to:

  • Net: WindowsBase
  • Net: WindowsFormIntegration
  • Net: PresentationCore
  • Net: PresentationFramework
  • Project: CustomControls

Step 3: In ExcelMainAddIn select “Add New item” and add “Ribbon (Visual Designer)” (Call it RibbonMain)

Add a ToggleButton to the ribbon.

Add a click event to the ToggleButton.

Step 4: Replace RibbonMain.cs with this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms.Integration;
using System.Windows.Controls;

namespace ExcelAddInTaskPanes
{
public partial class RibbonMain : ExcelRibbon
{
internal CustomTaskPane m_CustomTaskPane;
public RibbonMain()
{
InitializeComponent();
}

private void RibbonMain_Load
(object sender, RibbonUIEventArgs e)
{
// use a WPF control host
ElementHost _ElementHost;
_ElementHost
= new ElementHost();
_ElementHost.Child
= new CustomControls.UserControl1();

// stick the WPF host into a user control
UserControl _UserControl;
_UserControl = new UserControl();
_UserControl.Controls.Add(_ElementHost);

// make the user control the custom pane
m_CustomTaskPane
= Globals.ThisAddIn.CustomTaskPanes.Add
(_UserControl, "My Title");
}

private void toggleButton1_Click
(object sender, RibbonControlEventArgs e)
{
m_CustomTaskPane.Visible
= (sender as RibbonToggleButton).Checked;
}
}
}



Step 5: Make ExcelMainAddIn your start up project


Step 6: Hit F5


Now when it runs, Excel will load automatically. Your custom ribbon will appear in the AddIn tab on the ribbon bar. When you click your button your custom task pane with your control should appear. Click it again and it should disappear.


How about that? Easier than you thought.