In this Walkthrough we will use C# and Visual Studio 2012 to create a custom Excel menu and Custom Task Pane. You can use any other .NET language and development environment to achieve the same result in a similar way. F# based Walkthrough using FCell embedded code editor can be found here.

Create a C# class library project

  1. Open Visual Studio 2012

  2. Go to File->New->New Project

  3. Select .NET 4.5 as Target Framework

  4. Select a template for C# Class Library Project

  5. Select project location, enter CSharpRibbon as name and click OK

    VSProject Ribbon

Implement Ribbon customization

Create class for Ribbon and Custom Task Pane

  1. Add reference to FCell.ManagedXll (located in FCell installation folder), System.Windows.Forms and System.Drawing to the project.

  2. Rename Class1 in your project into TestRibbon

  3. Add Ribbon and Task Pane implementation

    C# Copy imageCopy
    using System;
    using FCell.Ribbon;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace CSharpRibbon
    {
    
        [ComVisible(true)]
        public class MyTaskPane : UserControl
        {
        }
    
        [ComVisible(true)]
        public class TestRibbon : XlRibbon
        {
            public override string GetCustomUI(string RibbonID)
            {
                var xml = @"
                            <customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui' onLoad='OnLoad' loadImage='GetImage'>
                            <ribbon>
                            <tabs>
                                <tab id='MyTabId' label='MyMenu'>
                                <group id='MyGroupId' label='MyGroup'>
                                    <button id='MyButtonId' imageMso='PageMenu' label='MyButton'
                                    size='large' onAction='OnButtonClick' />
                                </group>
                                </tab>
                            </tabs>
                            </ribbon>
                            </customUI>";
                return xml;
            }
    
            public override void OnCTPFactoryAvailable()
            {
                var ctp = this.CreateCustomTaskPane(typeof(MyTaskPane), "MyTaskPane");
                ctp.Visible = true;
            }
    
            public void OnButtonClick(IRibbonControl control)
            {
                MessageBox.Show("Hello from MyButton");
            }
    
            //optionally override GetBitmap if not using built-in imageMso to define button image
            public override Bitmap GetBitmap(string bitmapName)
            {
                //return here image 'bitmapName' for a button
            }
        }
    }

    You can create a Custom Task Pane simply by creating a Windows UserControl and calling CreateCustomTaskPane in an override of OnCTPFactoryAvailable

    Ribbon customization is defined in a class which inherits from XlRibbon (defined in FCell.ManagedXll.dll). GetCustomUI should return xml which defines your Ribbon customization. Note how you can define a button click event handler by simply adding a public member to the class.

Load custom Ribbon into Excel

Expose assembly as Excel customization

  1. Build the project in Debug

  2. Open Excel and go to FCell Ribbon

    FCell Menu
  3. Click on Excel .NET AddIn, add CSharpRibbon.dll and click Save

    CSharp Ribbon
  4. Click on Reload Excel AddIn, then find MyMenu on Excel Ribbon and click MyButton

    CSharp Ribbon My Button
  5. Go back to C# code and modify your button click event handler

    C# Copy imageCopy
    public void OnButtonClick(IRibbonControl control)
    {
        MessageBox.Show("Hello from updated MyButton");
    }
  6. In AssemblyInfo.cs specify AssemblyVersion as "1.0.0.*" to force version change at compilation

    Assembly Info
  7. Build the project

  8. Go to Excel, hit Reload Excel AddIn and click the button again and verify the new event handler fires:

    CSharp Ribbon My Button Updated

Debug Ribbon customization in Visual Studio

Attach Debugger to Excel

  1. In Visual Studio go to Debug->Attach to Process and attach to Excel.exe

    Attach To Process
  2. Add a breakpoint in your button event handler

    CSharp Ribbon Breakpoint
  3. Click MyButton in Excel and verify that breakpoint was hit in Visual Studio:

    CSharp Ribbon Breakpoint Hit