Outlook Developer Home
Outlook Programmer's Library
Outlook Technologies
Outlook Forms
Other Technologies



TIP: Fun with Outlook CommandBars

Many Outlook features simply don't have programmatic equivalents. A workaround in many cases is to execute the appropriate Outlook toolbar or menu command using the CommandBars object from the Office library. Each command has an ID associated with it. Use the FindControl method to locate the CommandBarButton, then the Execute method to run it.

For example, this code -- designed for use in VBScript on an Outlook form -- displays the Insert Attachment dialog:

You can also work through the CommandBars menu hierarchy by specifying the name of each menu and using its Controls collection in turn. Using FindControl is more efficient, though. However, in some cases, such as setting the sending account or creating a message with stationery, you use the ID to get the parent menu or command, but then must look at the text of the individual submenu commands to determine which to execute. The Set the sending account and Create new message with stationery samples demonstrate this variation.

Note that CommandBars is an object in the Office object model, not the Outlook object model. If you're using VB or VBA, make sure your project has a reference to the Office object library, then try one of the samples listed below. An important difference between Outlook and other Office programs is that the Outlook Application has no CommandBars collection. Instead, CommandBars is a member of the Inspector and Explorer objects. 

Samples | CommandBarButton IDs | Notes | More Information


Listing 21.9 in my Microsoft Outlook Programming book (sample code available) shows how to check the sending account in Outlook 2002/3 when the user sends a message and give the user an opportunity to change it.

Back to Top

CommandBarButton IDs

To use the FindControl method, you need to know the command's ID. For example, the ID for the command to switch to Outlook Today is 5599.

You can use Outlook Spy to look up the ID. Or, you can use your own code to recurse the Explorer.CommandBars and Inspector.CommandBars collections. For an example of such code, see:

Back to Top


Once you return the CommandBarButton with FindControl, you can do other things with it as well, such as hide it or disable it if you want the user to use a button on your custom Outlook form instead. Note, though, that changes to the button UI will affect all other Outlook Inspector or Explorer windows. Therefore, if you disable a button when a custom form opens, you need to re-enable it when the form closes. This Outlook form VBScript code shows how to disable and re-enable the Print menu command and Print button:

Back to Top

More Information

2002-7 TurtleFlock, LLC
All right reserved.
About OutlookCode.com

Send comments to webmaster@outlookcode.com

Designed and Implemented by TerraLink USA 

FrontPage Integration
by RobsonDesignWorks

get Sue's code | forums | share your own code | registration