Posted by Jack on 2nd April 2009

How to add macros to the Excel 2007 Ribbon

As much as I dislike using Microsoft products, sometimes, you have no choice. In our work with salesforce.com, the Excel Connector is a huge time saver. We use it for a number of things, and it is far superior to the data import functionality of salesforce.com.

In the upgrade from Office 03 to Office 07, custom toolbars were lost. I spent quite a while looking for the ‘custom ribbon’ in Excel 07 – it does not exist. Here is what you need to do to build a tab in the ribbon, add buttons for your macros, and embed both the customisations and the macros in an add-in (for deployment). I still have a few little changes to make, but everything is working with it set up like this.

  1. Create a new workbook in Excel
  2. If you do not already have your macros recorded, do that. If you do have them recorded, go into VB mode and copy the module(s) into your new blank workbook. Save the workbook as <add-in name>.xlam
  3. Download and install the Custom UI Editor Tool
  4. Start the custom UI Editor, and open your .xlam file. Now you can start customising the appearance of the ribbon. My code looks like this at the moment:
  5. <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon startFromScratch="false">
    <tabs>
    <tab id="BienaltoCustomTab" label="Bienalto">
    <group id="BienaltoSDDCFormulae" label="SFDC Formulae">
    <box id="Formulaebox1" boxStyle="vertical">
    <button id="Button6" size="normal" label="CampaignMember" onAction="AssignCampaignMembercallback" />
    <button id="Button7" size="normal" label="Lookup Email" onAction="sfemailcallback" />
    <button id="Button8" size="normal" label="Lookup Account" onAction="sfaccountcallback" />
    </box>
    <separator id="FormulaeSeparator1"/>
    <box id="Formulaebox2" boxStyle="vertical">
    <button id="Button9" size="normal" label="Lookup Firstname Lastname" onAction="FirstLastcallback" />
    </box>
    </group>
    <group id="BienaltoTables" label="SFDC Tables">
    <button id="Button1" size="normal" label="Contact" onAction="ContactTablecallback" />
    <button id="Button2" size="normal" label="CampaignMember" onAction="CampaignMemberTablecallback" />
    </group>
    <group id="BienaltoCase" label="Change Case">
    <button id="Button3" size="normal" label="UPPER CASE" onAction="UpperCasecallback" />
    <button id="Button4" size="normal" label="lower case" onAction="LowerCasecallback" />
    <button id="Button5" size="normal" label="Title Case" onAction="TitleCasecallback" />
    </group>
    </tab>
    </tabs>
    </ribbon>
    </customUI>
    
  6. Once you have edited the xml for your ribbon customisations, save and exit the Custom UI Editor Tool, and start excel. Next, activate your add-in. That code should get you something that looks like this:
  7. Custom Ribbon

  8. Here is an example of how each of my macros is coded. Note the main macro, and the smaller one above that references it:
  9. 'Callback for Button7 onAction
    Sub sfemailcallback(control As IRibbonControl)
    sfemail
    End Sub
    
    Sub sfemail()
    ActiveCell.Formula = _
    "=SFEMAIL(RC[1])"
    End Sub
    
  10. Once you are happy with it, you can distribute your .xlam file, complete with macros and ribbon enhancements. All the recipient needs to do is activate the add-in.

To be completely honest, I don’t fully understand the reason for using callbacks in macros. I believe it is so that you can continue referencing the original macro elsewhere (without the “control As IRibbonControl” bit).

The ribbon has copped a lot of hate since its launch, but I don’t think it’s all that bad. I’m actually getting used to it. While the process of customising it is challenging (to say the least), I think the results are pretty professional. You have far more power than is demonstrated in my example.

It looks like you can get pretty creative in your ribbon design – there are plenty of options for button types, images, etc on this page. It took me long enough to get it this far though :)

UPDATED:

Here is a template to use in the custom UI editor. Replace “onAction” text with callbacks to your macros, and change the label to suit.

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="false">
<tabs>
<tab id="BienaltoCustomTab" label="Bienalto">
<group id="BienaltoSDDCFormulae" label="SFDC Formulae">
<box id="Formulaebox1" boxStyle="vertical">
<button id="Button1" size="normal" label="Button Text" onAction="Button1MacroCallback" />
<button id="Button2" size="normal" label="Button Text 2" onAction="Button2MacroCallback" />
<button id="Button3" size="normal" label="Button Text 3" onAction="Button3MacroCallback" />
</box>
<separator id="FormulaeSeparator1"/>
<box id="Formulaebox2" boxStyle="vertical">
<button id="Button4" size="normal" label="Button Text 4" onAction="Button4MacroCallback" />
</box>
</group>
</tab>
</tabs>
</ribbon>
</customUI>

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

    13 Responses

  1. Jack says:

    I cannot believe that this article is #3 in Google for “add macro to ribbon”.

    I am amazed that it is ranking higher than anything from Microsoft, and is less than a month old.

    It has been very popular, but I haven’t heard any feedback – is it working for you?

  2. Marc says:

    Jack, Tried your approach, worked well/thanks.
    Also, found out that you can’t add buttons to a built-in group on a built-in tab.
    Cheers, Marc

    • Jack says:

      Hey Marc, I’m glad it worked for you. I didn’t really look into editing the built-in elements, so I’m not much use there sorry!

  3. mike says:

    How do I activate the Add-in ribbon?

  4. Matt says:

    Jack, I’ve got a real problem when attempting to use your approach. The problem is that when I click the button I get the following error “Cannot run the macro ’sfemailcallback’. The macro may not be available in the workbook or all macros may be disabled.” Now I get this error when the sfemailcallback subroutine, you provided as an example, lives on my .xlam add-in. However if it lives on the Book1.xlsx in the ThisWorkbook the code executes if I alter the XML onAction to include the ThisWorkbook.sfemailcallback call. Could you advise what I’m doing wrong? I’ve followed your directions it just appears and those on many other websites, however I’ve not met with much luck. At this point my XML is as follows:

    and my VBA is as follows:

    Sub MyMacro(control As IRibbonControl)
    test
    End Sub

    Sub test()
    MsgBox (“Test”)
    End Sub

    Both are on exist on the same .xlam as specified by your instructions.

    Please any help you can offer would be greatly appreciated.

  5. Matt says:

    it appears my XML got cut off please email for the the specifics.

  6. Jack this was my first time coding the ribbon in office. A hundred million thanks for your code. Could not find the proper way anywhere to code the onAction section of the button, and kept getting errors.

    Thanks ever so much for sharing your code.

    Kindest regards,

    Alfred

    • Jack says:

      Hi Alfred, I’m glad it worked for you. I still cannot believe how difficult it is to do this compared to Office 03. The result looks good though!

  7. Foster says:

    For some reason, when I save the new workbook as an .xlam file and open with the editor, nothing displays in the right window. What am I doing wrong?

    • Jack says:

      Hi Foster, it will be empty until you modify it. Start with my example code above, that should set up a basic template for you to edit. Let me know if you would like more help.

  8. Foster says:

    I’ve added the code to the .xlam file, saved and enabled the add-in in Excel 2007. My macro has the callback method, but I don’t get a new Ribbon displayed. Am I missing a step?

  9. Katrin Boermans says:

    Jack, you rock my Excel world, thanks for the usefull and clear explanation!

Post your comments