VBA Ribbon Access
VBA Ribbon
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 ../../Shorts/Accelerators/Accelerators.php], I stumbled over this blog post by Pranav Waghthis blog post by Pranav Wagh [link to blog at 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 MSDN at 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 ffor 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:
[link to the file on this site at Files/RibbonDemo.dotm]
I have been pleasantly surprised at the response to this page, but have been asked several times how my code can be adapted to automatically switch Ribbon Tabs when a document is opened. Rather than replying to individuals directly, I have decided to make another file available for downloading from here. This sample does not have the UserForm that the first one has, but it does have code in the Document_New and Document_Open Events, that switches to the Review tab; the tab name is hard coded, but easy to see, and change.
You can download this version from here:
[link to the file on this site at Files/TabSwitch.dotm]