Andrei Smolin

How to navigate through the Microsoft Office Command Bar system

There are 4 instances of the command bar button “Save” (Id=3) in Excel. Two of them are best known in Excel 2000-2003: a button on the Standard commandbar and an item in the File menu. The other two instances are buried in the command bar system; Excel shows them when its logic requires. Now, the “Cut” button has 32 (thirty two!) instances. How to determine which of the instances is clicked by the user?

Note on Excel 2007-2010. In these Excel versions, the command bar system is hidden but it still functions: when you select a cell and press {ALT+E} and then {T} in Excel 2010, this invokes one of the CommandBarButton “Cut” instances. You can check this using the add-in project availiable below.

The CommandBarButton.Click event provides a parameter of the CommandBarButton type. The parameter reflects the feature of the Office object model: intercepting the Click event of a given instance of a built-in command bar button – remember the ID of such a button is greater than 1, ID>1 – generates the Click event whatever instance of that button is clicked. That is, this parameter allows finding out which of the button instances is clicked.

Add-in Express uses all this in the Built-in Control Connector component. The component allows intercepting the Click event of a built-in commandbar button. You can request that the component intercept the Click event of any instance of a CommandBarButton with a given ID. Or, you can specify that the Built-in Control Connector intercepts clicks on the built-in button only if it resides on a particular command bar.

The add-in project accompanying this article demonstrates how use the Built-in Control Connector component to find the path to a clicked control in the command bar system.

The sample add-in intercepts and cancels clicks on the “Cut” button (Id=21). To intercept some other built-in control, you must specify its ID in the Built-in Control Connector component; you can find the ID of every built-in control using our free Built-in Control Scanner.

Good luck!

Available downloads:

These sample COM Add-ins were developed using Add-in Express for Office and .net:

C# sample COM add-in
VB.NET sample COM add-in

Post a comment

Have any questions? Ask us right now!