Search code examples
c#excel-dna

Connecting Excel-DNA button to C# code


How does one connect Excel-DNA ribbon controls to functions in C# code?

My DnaLibrary file (.dna) contains a ribbon with buttons. But I don't know how to get C# code to fire when pressed. The OnButtonPressed function shown below is never called.

Here is my CustomUI XML:

<CustomUI>
    <customUI xmlns='http://schemas.microsoft.com/office/2009/07/customui' loadImage='LoadImage'>
      <ribbon>
        <tabs>
          <tab id='XLST_Tab' label='XLST'>
            <group id='XLST_Group' label='Standard Time'>
              <button id='BigBtn' label='Press Me' size='large' />
              <button id='BigBtn2' label='Press This Too' size='large' />
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>
</CustomUI>

Here is my C# code:

namespace XLST
{
    [ComVisible(true)]
    public class XLSTRibbon : ExcelRibbon
    {
        public void OnButtonPressed(IRibbonControl control)
        {
            MessageBox.Show("Hello from control " + control.Id);
        }
    }
}

Solution

  • You also need to set the onAction attribute in the ribbon xml:

    <button id='BigBtn' label='Press Me' size='large' onAction='OnButtonPressed' />