VBA Ribbon Access
VBA Ribbon
If you have any problems with this web page, please first read my browser notes and if you still have issues, feel free to e-mail me.
It can hardly have escaped your notice that Word 2007 has a new interface, that the old Menus and Toolbars have been replaced by a new Ribbon. All the things that you could do with the old interface have gone, replaced by all the things you can do with the new interface; except that there isn’t anything you can do with the new interface: not in VBA, anyway. That is probably what you have been told but it isn’t entirely true.
Whilst investigating customising the legacy keyboard supportcustomising the legacy keyboard support [Link to page Shorts/Accelerators/Accelerators.htm on this site], I stumbled over this blog post by Pranav Wagha blog post by Pranav Wagh [Link to http://blogs.msdn.com/pranavwagh/archive/2008/01/21/how-to-switch-ribbon-tab-programmatically-looks-simple.aspx]. As I’m not very good with .NET, and Visual Studio and its languages (I’m trying to teach myself but it’s a slow process) I thought I’d see what could be done in VBA. What do you know, the Ribbon is accessible without having to leave home.
Microsoft® Active Accessibility® is a standard interface made available to developers of accessibility aids, software and hardware designed, to quote Microsoft, to provide additional accessibility [to computers] to individuals who have physical or cognitive difficulties, impairments, and disabilities. It has been around for a long time and, to quote Microsoft again, exposes information about most system-provided user interface elements.
There is actually a new, richer, interface – User Interface Automation – designed to replace Active Accessibility but, I don’t think Active Accessibility is likely to disappear any day soon, and it should be possible to rely on it for some time to come.
The Ribbon is, as per the above description, a system-provided user interface element, and information about it is, indeed, exposed via the Accessibility interface. Perhaps surprisingly, it is exposed to VBA, so there is no need to even leave the comfortable Word environment to see it.
The Accessibility interface offers a hierarchy of objects subordinate to the main window of the (Word) application. You can explore them all by getting a handle on that main window, but there is a short cut to the Ribbon object in the middle of the hierarchy; there is still quite a complex hierarchy of objects to navigate but you do know you are starting in the right place. The shortcut is provided by Word because, behind the scenes, the Ribbon is hiding in a familiar CommandBar object and, although you may never have had a use for it before, CommandBar objects have always offered an Accessibility interface.
So what is it that you have access to, what exactly is an Accessibility object? Simplifying somewhat, for expediency, there is one such object for each element of the User Interface, and they come in various types representing the various types of things you see on the screen. The Ribbon CommandBar Accessibility object is a ToolBar object; no real surprise there, and its actual content is displayed in the Client area of a subordinate Window. Further down the hierarchy there is a Window, the Ribbon window proper, as it were, called Ribbon, containing a Property Page, also called Ribbon, and it is within this that all the juicy detail can be found.
Amongst other children, the Ribbon Property Page contains a Page Tab List, called Ribbon Tabs, that, in turn, contains a Page Tab object for each Tab, and a Pane, called Lower Ribbon, that contains a Property Page for the current Tab; this Property Page contains a Toolbar object for each Group, which Toolbars contain the various objects you see on screen. That brief description probably sounds like gobbledegook if you are not already familiar with any aspects of it; I may return and provide more detail here, but if you can't wait, then this page on msdnthis page on msdn [Link to Microsoft page http://msdn.microsoft.com/en-us/library/bb404170.aspx] is as good as anywhere to start reading.
I have put together a sample template to demonstrate the technique. What started out as a simple demonstration has evolved into, almost, a ribbon-in-a-box but please remember it is just a demonstration and is not designed to be functional or useful on its own. I have tried to make the code neat and have commented it; feel free to study it at your leisure and adapt it for your own use.
It is built to be used as a global template, to be put in your Word StartUp folder, but that is not essential for it to work. The template has an icon on the QAT to invoke its code; it should appear at or near the end of your QAT depending on how you open it and what else you are running and has an icon showing three flags, the closest I could find to a ribbon.
You can download it from here:
[Button with link to the file at Files/RibbonDemo.zip]
Tony Jollans. Page last updated 8 September 2008.