Create an ADD-IN with Ribbon buttons that run macros when clicked
When Microsoft introduced Office 2007, they created a totally new user interface that requires a totally new way of adding customizations (ie, new buttons that run macros) to PowerPoint.
Actually, the old way of doing things still works reasonably well. When you write code to create command bars with buttons as explained in Create an ADD-IN with TOOLBARS that run macros, PowerPoint 2007 and later add a new Add-Ins tab to the ribbon and create any requested command bars and buttons there.
But if you want to take advantage of the new Ribbon interface, you'll need to do things a bit differently. You'll need to write special XML code to define the buttons you want to create, what tabs they appear on and what subroutines they'll run when the user clicks them.
Before we get into the details, let me recommend RibbonX: Customizing the Office 2007 Ribbon (Ken Puls/Robert Martin/Teresa Henning). While you can find a lot of the material in this book on the internet, you won't find it so neatly packaged and organized nor will you find a clearer or more complete reference. If you plan to do more than the simplest possible Ribbon customizations, do yourself a favor: get this book. While the title mentions Office 2007, all of the information applies equally well to Office 2010 and beyond.
Create and add XML to your project
You can create RibbonX in Notepad and use Windows to unzip your PPTM, add the XML to it, and re-zip it. You can also perform your own tooth extractions with a pair of pliers. I wouldn't wish either upon you and don't plan to offer "how-to" instructions for either here.
You'll want to download the Custom UI Editor tool (see link below) or one of the other available RibbonX XML editors. They'll make the job more efficient, validate your XML for you and make sure that it's popped automatically into the correct part of your PPTM/PPAM files.
First, create a presentation, add some VBA code to it and save it as a PPTM, then close the file.
Here's the code to add for our example exercise:
Public Sub RibbonXDemo_BUTTON_One() ' This just tells you how many slides are in the current presentation Dim oPres As Presentation If Presentations.Count < 1 Then MsgBox "Please open a presentation and try again." Exit Sub End If Set oPres = ActivePresentation MsgBox "Button_One says that there are: " & CStr(oPres.Slides.Count) & " slides." End Sub ' BUTTON_One Public Sub RibbonXDemo_BUTTON_Help() ' This one's even more useless than the first MsgBox "Help? HELP??? At these prices, you expect HELP?????" End Sub
Then open the PPTM in the Custom UI Editor tool or another RibbonX editor and add this XML:
<!-- This adds a new group to the Design tab and adds a couple of buttons to the group --> <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"> <ribbon> <tabs> <tab idMso="TabDesign"> <group id="RibbonXDemo_Group_RibbonXDemo" label="RibbonX Demo" insertAfterMso="GroupPageSetup"> <button id="RibbonXDemo_Button_One" label="Button One" size="normal" imageMso="DrawingCanvasExpand" onAction="RibbonXDemo_Button_One" screentip="Do whatever Sub RibbonXDemo_Button-One says to do" /> <button id="RibbonXDemo_Button_Help" label="Help" size="normal" imageMso="Help" onAction="RibbonXDemo_Button_Help" screentip="Help me! Help me! HELP!!!" /> </group> </tab> </tabs> </ribbon> </customUI>
Save and close the file. Open it again in PowerPoint and click the Design tab. If you've done everything right, there'll be a new RibbonX Demo group to the right of the Page Setup group. The new group will have two buttons. Hover the mouse cursor over them to see the tooltip text, click them to see what they do (not much, just as promised).
Tricks, Tracks, Tips
Give your subroutines unique names
You'll notice that the names of the IDs, buttons and subroutines in both the XML and the VBA all start with the same "RibbonXDemo_" prefix.
It's a good idea to make these names unique. If you use the same name ( Sub Help for example) in several different add-ins, or if someone else has used the same name in their add-in, PowerPoint may run the wrong code when your Ribbon button is clicked.
XML in a PPTM vs PPAM
By now, you've probably noticed that your new custom group on the Design tab and all of its buttons disappear when you close the file containing the XML and VBA. This may be useful under some circumstances, but generally, no.
You'll probably want to save the file as an add-in:
Choose File | Save As | Save as type: | PowerPoint Add-In (*.PPAM)
Don't choose PowerPoint 97-2003 Add-in (*.PPA). The add-in will still work but there won't be any RibbonX code in it. No RibbonX code, no buttons to click, so no way to RUN the code. (Well. Strictly speaking, there may be, but it's not very practical, so let's pretend that there's not.)
Now you can close the file and use File | PowerPoint Options | Add-Ins to load the newly saved add-in. Your custom group and its buttons will re-appear and will become part of PowerPoint until you choose to remove the add-in again.
RibbonX editors and other resources
- Learn the basics about the new ribbon interface here and here and here and here.
- Custom UI Editor Tool and other resources
- Andy Pope's Ribbon Editor
- The Ribbon Designer for Visual Studio 2013