Image of ribbon tap
A new ribbon tab with large buttons and a screen tip

Hands on: Master the Office 2007 Ribbon

How to make your macros work with Office 2007

Written by Tim Anderson

Microsoft Office 2007 replaces most of the menus found in earlier versions of Office with a new ribbon control.

From the user’s perspective, the ribbon cannot be customised, but developers have full control. If you have existing Word macros that use menus or toolbars, they usually work in Office 2007, but the implementation is ugly.

Any custom menus get moved to the Add-Ins ribbon tab. What follows is a look at what it takes to revise an existing macro to work properly with the Office 2007 ribbon. The macro integrates media playback into Word so that you can play, pause and navigate an audio file through toolbar buttons or keyboard shortcuts. It’s ideal for transcribing audio, since you can control the sound without reaching for the mouse.

The macro is written in VBA and stored in a Word template. In Word 2007 it works, but the buttons are mixed with those for other compatibility macros on the Add-Ins ribbon.

VBA versus .NET
You may wonder if the VBA macro must be converted to C# or Visual Basic .Net. Fortunately, it’s not necessary, despite Microsoft’s focus on .NET. In fact, it is better to leave it in VBA unless there is a compelling reason to convert it. The Office Ribbon API is Com based, and VBA works well with Com.

Leaving the macro in VBA avoids the overhead of loading the .Net runtime and means that code has no need to cross the Com/.Net boundary.

Converting the template
The first step is to convert the template to the Office 2007 format. Open the old template in Word, and save as a Word macro-enabled template with a .dotm extension. Alternatively, you could start a new macro-enabled template and paste the old VBA code into it. You now have a template in the Open Office XML format.

To customise the ribbon, you need to specify the customisations in an XML file and embed this in the new template according to Microsoft’s XML Packaging specification. Here is an example:
<?xml version=”1.0” 4 encoding=”utf-8” ?>
<customUI
xmlns=”http://
schemas.
microsoft.
com/office
/2006/01/
customui”
onLoad=”This
Document.Ribbon
Loaded”>
<ribbon
startFromScratch=”false”>
<tabs>
<tab id=”MediaTab” label=”PCW
Media”>
<group id=”WordMedia”
label=”Word Media”>
<button id=”btnPlay” label=”Load” keytip=”A”
screentip=”Load and play an
audio file ALT+L” imageMso=”HappyFace”
onAction=”ThisDocument.Playx”/>
</group>
</tab>

This document creates a new tab in the ribbon with the label PCW Media. The tab contains one group, and the group contains one button. The button has the built-in image HappyFace as a placeholder. Normally you will set your own image, using an image attribute rather than imageMso.

When loaded, the ribbon calls a VBA routine called RibbonLoaded, and when the button is clicked it calls another routine called Playx. This is VBA code that you write in the normal way. You can write this XML with any editor, even Notepad, though the steps to include it in the document are arduous.

It involves renaming the .dotm file with a zip extension, extracting the zip , copying the XML file into a new subdirectory within the zip archive and editing the top-level .rels file to include the new XML content. This becomes more arduous if you add images to the template, since these require further sub-directories and a new .rels file, according to the Open Packaging Convention.

Fortunately there is an easier method. You can download a utility called the Office 2007 Custom UI editor. As an XML editor it’s basic, but it does handle the packaging aspect for you, saving a lot of time. It will also validate the XML against the correct schema. A disadvantage is that the UI editor has no pop-up help or auto-completion.

You might want to take a hybrid approach, using a better XML editor such as Visual Studio 2005. Create a new XML document, and set the schemas property to point to CustomUI.xsd, which gets installed with the Custom UI editor. This gives you pop-up help as well as validation. Once done, you can copy & paste the XML into the Custom UI editor for packaging.

Open the template in Word 2007. You will get errors if you have not yet written the VBA functions, but it should show a new ribbon tab with the controls as defined in the XML (see the attached PDF). A variety of controls are possible, though this example uses only two: a standard button and a toggleButton.

reader comments

related articles

 

today's top stories

Analysis: The true cost of printing

Organisations need to get a better sense of how much they spend on printing before finding ways to reduce it 05 Sep 2008

Computing podcast 4 September 2008

Find out what Michael Dell told Computing, and listen to our take on the latest browser wars 04 Sep 2008

Looking to the future - exclusive Michael Dell interview

Dell's chief executive talks to Computing about the way the company continues to adapt to major changes in the industry 04 Sep 2008

Interview: Delivering power where it's needed at Betfair

The online gambling firm is putting its money on grid computing and virtualisation to underpin global expansion 04 Sep 2008

E-paper displays are an open book

A display revolution is on the way - but only once the user interface issues are solved 04 Sep 2008

Most commented stories

Advertisement

Newsletter signup

Sign up for our range of FREE newsletters:

Existing User

Newsletter user login:

Jobs

Related jobs

Job of the week

Job alerts

Sign up here

Find your next job

Advertisement

White papers

Search white papers

Top categories

VPN, Extranet and Intranet Solutions

WAN/ LAN Solutions

Network Security

Interoperability-Connectivity

Grid/ Utility Computing

Latest poll

Would you use a mobile phone as an alternative to cash?

Would you use a mobile phone as an alternative to cash?

When mobile phones include inbuilt payment technology - would you use one instead of cash?

Previous poll results

Latest audio and video articles

BlackBerry BoldVideo

Video Review: BlackBerry Bold

Technology editor Daniel Robinson takes a hands-on look at the latest device from Research in Motion 01 Sep 2008

Podcast imageAudio

Computing podcast 4 September 2008

Find out what Michael Dell told Computing, and listen to our take on the latest browser wars 04 Sep 2008

Latest in-depth articles

A meetingAnalysis

Turning adversity into an advantage

IT chiefs under pressure to make cost cuts can turn the situation to their benefit 04 Sep 2008

CloudAnalysis

How to introduce cloud computing into your organisation

Best practice advice from Forrester Research 04 Sep 2008

Primary Navigation