Search code examples
c#exceladd-inexcel-addins

How to intercept clicking of a built-in Office Ribbon control


I'm wondering if it's possible to detect when a user has clicked the Header/Footer button in Excel so I can show some custom header/footer related ribbon controls on my add-in's tab and hide them when the user is not in header/footer edit mode.

Is it possible to hijack this button click somehow? I've seen applications hijack the Excel Save before. I'm looking for similar behavior just with the header/footer button.

I'm using C#, Visual Studio 2012 and Excel 2010. I've created my custom ribbon using the Ribbon XML approach.


Solution

  • One way I've discovered that worked was to use the <commands> section in the Ribbon XML (which I didn't know existed.) Apparently this mechanism allows you to re-purpose actions intrinsic to Excel but beware that not all controls support re-purposing the onAction callback)

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
        <commands>
            <command idMso="HeaderFooterInsert" onAction="testHeaderFooter"/>
        </commands>
      <ribbon>
        <tabs>...
    

    along with the associated event handler:

        public void testHeaderFooter(Office.IRibbonControl control, bool cancelDefault)
        {
            MessageBox.Show("Testing.");
            cancelDefault = false;
        }
    

    This link was very helpful:

    http://social.msdn.microsoft.com/Forums/office/en-US/e1a60d16-053e-4697-b17c-b22d602f0400/intercept-the-onaction-event-of-a-gallery-element-of-excel-2007-ui-ribbon?forum=vsto