Andrei Smolin

How to intercept clicking an Office Ribbon control or command bar button

There are three ways to invoke a command of an Office application:

  • Click a built-in Ribbon button (2007-2010);
  • Click a built-in CommandBar button (Office 2000-2003 and some Office 2007 applications);
  • Press a key combination

How to intercept clicking a built-in Ribbon control

In the Ribbon UI, you can intercept invoking these control types:

  • Ribbon button
  • Ribbon toggle button
  • Ribbon checkbox

To intercept clicking a built-in Ribbon button such as File | Save in Word, Excel, PowerPoint etc., you need to find out the button’s IdMso, and use it to set up a Ribbon Command component; the component fires the OnAction event when that command is invoked.

A built-in Ribbon control is identified by its IdMso. IdMso is a string published by Microsoft in 2007 Office System Document: Lists of Control IDs and Office 2010 Help Files: Office Fluent User Interface Control Identifiers. These downloads provide a set of Excel files such as ExcelControls.xlsx and OutlookMailReadItemControls.xlsx; the Control Name column of each contains the IDs of almost all built-in Ribbon controls for the corresponding Ribbon.

To intercept invoking a command, you add a Ribbon Command component onto the add-in module and indicate the IdMso of the built-in Ribbon control to intercept in the component’s IdMso property:

The component fires the OnAction event when the user invokes the Ribbon command specified in the IdMso property:

C#:

private void ribbonCommandFileSave_OnAction(object sender,  IRibbonControl control, bool pressed,  ADXCancelEventArgs e)
{
    MessageBox.Show("You've invoked the Ribbon command FileSave ");
    e.Cancel = true;
}

How to intercept clicking a built-in CommandBar button

In the CommandBar system, you can intercept clicking buttons; other control types are unavailable for intercepting.

To intercept clicking on a CommandBar button such as File | Save in Word, Excel, PowerPoint etc., you find out the button’s Id (not IdMso!), and use it to set up a Built-in Control Connector component; the component fires the OnActionEx event when a control with that ID is clicked.

The ID of a CommandBar control is an integer value. For a custom CommnadBar control, it is always 1. The ID of a built-in control is set on the factory; you can find it using Built-in Controls Scanner – a free utility that lists the names, IDs, enabled and visible states of all CommnadBar controls in a specified Office application.

Now you add a Built-in Control Connector (ADXBuiltInControl) onto the add-in module and indicate the ID of the built-in control to intercept in the component’s ID property:

The final stage is to handle the OnActionEx event of the ADXBuiltInControl class:

C#:

private void CommandBarControlFileSave_ActionEx(object sender,  ADXCancelEventArgs e)
{
    MessageBox.Show("You've clicked the CommandBar control invoking the FileSave command");
    e.Cancel = true;
}

Find more details about how Built-in Control Connector (ADXBuiltInControl) operates in How to navigate through the Microsoft Office Command Bar system.

How to intercept pressing a key combination

Some key combinations end up in invoking the corresponding command bar or Ribbon control; that is you can intercept such keys using the approaches described above.

Note. The samples below are given according to Verklärte Macht: Keyboard Revisited by Jensen Harris.

This relates to Menu Accelerators e.g. ALT+O+H+R to navigate Format – Sheet – Rename to access the Format Sheet dialog box in Excel 2003. Pressing this combination in Excel 2007-2010 invokes a Ribbon command that you intercept in the way described in How to intercept clicking a built-in Ribbon control.

In the same way, Key Tips introduced in Office 2007 invoke corresponding Ribbon commands that you deal with in How to intercept clicking a built-in CommandBar button.

But there are keyboard shortcuts connected to the internals of the Office application itself. For instance, pressing Ctrl+S bypasses triggering the corresponding built-in commandbar  or Ribbon button. To intercept such keyboard shortcuts, you use the KeyBoard Shortcut component (ADXKeyBoardShortcut). You add it onto the add-in module, set the ShortcutText property to the shortcut you need to intercept and handle the OnAction event.

Note that ADXKeyBoardShortcut gulps the key press and doesn't pass it to the host application.

And here's an example of how you handle keyboard shortcuts in Word. The point is that Word allows enumerating keyboard shortcuts associated with a given command. So, the code fragment below creates ADXKeyBoardShortcut components intercepting the FileSave command:

C#:

List<ADXKeyboardShortcut> fileSaveShortcuts = null;
private void AddinModule_AddinInitialize(object sender, EventArgs e)
{
    object param = Type.Missing;
    Word.KeysBoundTo wordKeys = WordApp.get_KeysBoundTo(
        Word.WdKeyCategory.wdKeyCategoryCommand, "FileSave", ref param);
    List<ADXKeyboardShortcut> shortcutProcessors = 
        new List<ADXKeyboardShortcut>(wordKeys.Count + 1);
    for (int i = 1; i <= wordKeys.Count; i++)
    {
        ADXKeyboardShortcut shortcut = new ADXKeyboardShortcut(this.components);
        shortcut.ShortcutText = wordKeys[i].KeyString;
        shortcut.Action += new ADXAction_EventHandler(FileSaveShortcut_Action);
        shortcutProcessors.Add(shortcut);
    }
    Marshal.ReleaseComObject(wordKeys);
}
 
void FileSaveShortcut_Action(object sender)
{
    MessageBox.Show("You've pressed " + (sender as ADXKeyboardShortcut).ShortcutText);
}

You may also be interested in:

Intercepting clicking built-in command bar and Ribbon controls is also demonstrated in these articles:

How to handle Outlook item's Reply event: replying from a context-menu – intercepting the Reply button in a context menu of Outlook 2003;

How to create a custom event when Excel calculation mode changes – intercepting built-in controls (CommandBar and Ribbon) in Excel to get notified about calculation mode changes;

Excel shapes events: getting notified about user actions – intercepting the Group and Ungroup commands (CommandBar and Ribbon) that are used to group and ungroup Excel shapes.

Good luck!

 

8 Comments

Post a comment

Have any questions? Ask us right now!