MS-Excel / General Formatting

Step 1: Create a Macro-Enabled Office Document or Template

Because you'll be associating your custom Ribbon controls with macros, you need to begin with a macro-enabled file. You have several choices:

If you only want the custom Ribbon interface available for a specific document, create a macro-enabled document (.docm, .xlsm, or .pptm).

  • If you want the custom Ribbon interface available only for any document based on a particular template, create a macro-enabled template (.dotm, .xltm, or .potm).
  • If you want the custom Ribbon interface available for any open document in Word, modify the Normal template.
  • If you want the custom Ribbon interface available for any open document in Excel or PowerPoint, you need to create an Add-in file (.xlam or .ppam).

If you want to work with an add-in file, remember that you have to first create the file and then load it.To create the add-in, start a new document or open an existing document (such as one that has macros you want to include in the add-in). Choose Office, Save As to open the Save As dialog box, click Save as Type to choose the add-in type (Excel Add-In or PowerPoint Add-In), and then click Save. Note that you might want to store the file in the AddIns folder:

%UserProfile%\appdata\Roaming\Microsoft\AddIns

To load the add-in in Excel, choose Office, Excel Options and the click Add-Ins.Use the Manage list to click Excel Add-ins, and then click Go to open the Add-Ins dialog box. If you didn't save the add-in in the AddIns folder, click Browse, click the add-in, and then click OK.The procedure for loading PowerPoint add-ins is similar.

You can now add your macros to the document or template. Here's the general form to use:

Sub ProcedureName(ByVal control As iRibbonControl)
    [Statements]
End Sub
ProcedureName
A unique macro name.

control
An iRibbonControl object that represents the Ribbon control that was clicked to trigger the macro. You'll see later that you sometimes use this object to work with the control (for example, to determine which item in a menu was clicked).

Statements
The VBA code you want the macro to run.

Here's an example:

Sub MyButton(ByVal control As iRibbonControl)
    MsgBox "Hello Ribbon World!"
End Sub

In the language of custom Office 2007 interfaces, the macro that runs when you click an interface element is known as a callback.

When you're finished, save and close the document.

[Previous] [Contents] [Next]