Developer (advanced): Triggering a tab switch
Microsoft didn’t outfit Office 2007 with an Object Model to access the Ribbon. As developers, we are very limited in what you can do with the Ribbon. One of the things we cannot do for example is to switch a tab. Only the user (or Office itself) can control which tab is currently displayed on the Ribbon. As developers though we might encounter situations where we really want the user to have automatically a particular tab active on the Ribbon. One such example is if we want to build our own custom contextual tabs.
As Microsoft didn’t provide developers with the means to do this, we need to use a workaround: The add-in needs to pretend that it is the user and send the keystrokes to switch a tab to the Office application. What are these keystrokes? As an example, I will show you how to switch to the View tab in Word.
If you open Word and press and release ALT, it will show you the KeyTips. From the KeyTips, you can see that the View tab is triggered by ALT-W. If you press W then, you will see that Word switches to the View tab, but that the KeyTips are still displayed. To make them disapear, you can either press ESC twice or F6 once. From this example, we know that we need to send ALT-W followed by F6 to Word to switch to the View Tab.
In C# and VB.NET, we can accomplish this with the following line:
System.Windows.Forms.SendKeys.SendWait("%W{F6}");
% represents the ALT key in this example. To send F6 and not the characters F and 6, we need to enclose it in {}. We should use SendWait() instead of Send(), because we might have other commands we want to execute after the keystrokes have been sent to Word. SendWait will make our application wait until the keystrokes have been sent to Word instead of continuing right away no matter whether they were already sent to Word or not. In order for this to work, we need to make sure that System.Windows.Forms is in the References of the project.
In VB6 and VBA, the following line achieves the same:
SendKeys "%W{F6}", True
Let me know how this works for you! In a later post, I will expand on how we can use this trick to construct our own contextual tab.

May 28th, 2006 at 12:38
Thats a nice tip Patrick and works in Access as well. Going to try it out on a wider scale in Access.
May 28th, 2006 at 15:44
Hi Martin,
glad it works for you! If I don’t specifically state that something is limited to a particular application, it should work in all ribbon apps. I will generally pick an app to showcase something with randomly. Yesterday I felt like Word, tomorrow it might be Excel…
Patrick
May 29th, 2006 at 4:36
Hi Patrick,
Great to see someone doing this work!
My comment: of course this will have to be adapted to account for localised versions, as the shortcut keys will depend on the language of the UI.
May 29th, 2006 at 10:02
Hi Jan Karel,
you are correct. This has to be localized. I didn’t have a non-English version until today, so I couldn’t try this (I installed the German Language Pack).
Localization issues can be dealt with (to some degree) by enumerating the ribbon, but that’s a different story.
September 13th, 2006 at 10:36
Hi Patrick,
Thank you for your insights on this site.
Specifically concerning the keyboard-entry-simulation: Is there any hope that need of this will be obsolete with the release of Office 2007 or doesn’t Microsoft even plan on giving access on the Ribbon via the Object Model?
If not, it will be a problem for many businesses, I guess, since in commercial third-party products that want to activate a custom tab on load of a custom template — your backdoor solution won’t do, sadly.
I tried your above line of VBA in an AutoNew-Macro and it works quite well, except, that the Keyboard-Shortcuts stay activatet after the switch so that the pressing of further keys may unexpectedly trigger other shortcuts. That can be clumsily remedied with the addition of {Esc}{Esc}.
Tyll
September 13th, 2006 at 11:10
Hi Tyll,
The {F6} should do the same as {ESC}{ESC}. That didn’t work for you?
Unfortunately, this backdoor solution is all you will have available for Office 2007. Microsoft will not provide an object model for the ribbon in 2007, despite lots of beta testers pleading for one.
Patrick
September 20th, 2006 at 5:08
Yes, {F6} works as expected now — I couldn’t reproduce any strange behaviour but it seemed not to work on first try.
Thanks, Tyll
January 12th, 2007 at 6:12
You can also press then “Enter” key after the sortcut for the tab.
For example : press “Alt”, then “W” then “ENTER”, and the view tab is activated.
So replace {F6} by {ENTER} in the ‘SendKeys’ parameter
January 13th, 2007 at 17:46
There is a problem with the ‘SendKeys’ function :
it can not send just the ‘Alt’ key alone, followed by the keys we want for the custom tab.
For exemple, if the keyTip for our custom Tab is ‘C’, then if you write SendKeys (”%C{ENTER}”), then it just copies the selection (control-C) and hit then Enter key : not exactly what we want. Further more, if the keyTip is composed with several keys (like “JB” in my case), then you sould not use SendKeys(”%(JB){ENTER}”) nor SendKeys(”%JB{ENTER}”) : what we want is first pressing ‘Alt’ key (pressing down and releasing it), and then pressing the several letters of the keytip.
So I suggest to use API keyboard function. Here is an example :
=================================================================
Private Declare Sub keybd_event Lib “user32″ ( _
ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_KEYUP = &H2
Public Sub KeyPress(ByVal vbkey As Byte)
‘ This function simulates the action of pressing a key :
‘ First press it down, and then release it
‘ vbkey : argument like ‘vbKeyA’ (which is the ‘a’ key),
‘ or ‘vbKeyMenu’ (which is the ‘Alt’ key)
keybd_event vbkey, 0, 0, 0
keybd_event vbkey, 0, KEYEVENTF_KEYUP, 0
End Sub
Public Sub ActivateMyCustomTab
‘ Activate the proper Custom Tab of the Addin
‘ This Tab must have a known keyTip propriety
‘ (the keyTip propriety is designed in the ribbon XML file)
‘ Here, for example, the ‘keytip’ propriety for the Tab is “JB”
‘ Then, you can easily change, using your own keyTip
Application.Activate
KeyPress vbKeyMenu ‘ press the Alt key
KeyPress vbKeyJ ‘ press the “J” key
KeyPress vbKeyB ‘ press the “B” key
KeyPress vbKeyReturn ‘ press the Enter key
End Sub
May 27th, 2007 at 7:20
Patrick -
“Localization issues can be dealt with (to some degree) by enumerating the ribbon, but that’s a different story.”
Does enumerating the ribbon indicate what characters are used for a particular item? Is this story told elsewhere in the forum?