Image of ribbon tap
A new ribbon tab with large buttons and a screen tip
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Master the Office 2007 Ribbon

How to make your macros work with Office 2007

Tim Anderson, Personal Computer World 12 Feb 2008
ADVERTISEMENT

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.


All Software Applications
Tags: Visual Programming

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
Solihull, United Kingdom | Enzen Global Limited
Business Consultant - £35,000 - £40,000 - Solihull We are in need of a Business Consultant with strong analytical skills and a penchant for learning the domain knowledge of the Utilities sector (Gas industry in ... more >
Welwyn Garden City, Hertfordshire, United Kingdom | Tesco.com
Affiliate & Media marketing manager - Welwyn Garden CityWho's behind the world's most successful online retailer? Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales ... more >
Welwyn Garden City, Hertfordshire, United Kingdom | Tesco.com
Infrastructure Delivery Project Manager - Welwyn Garden City Who's behind the world's most successful online retailer? Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales ... more >
Cardiff, United Kingdom | University of Wales
Projects Officer - £26,665 - £30,912 - Cardiff The Projects Officer will work on specific projects under the direction of the Head of Information Services. It is expected that these will concentrate on the redevelopment ... more >
More job opportunities