Microsoft KB Archive/182394

= OL98: How to Use Command Bars in Outlook Solutions =

Article ID: 182394

Article Last Modified on 3/1/2004

-

APPLIES TO


 * Microsoft Outlook 98 Standard Edition

-



This article was previously published under Q182394



SUMMARY
This article provides an overview of using the CommandBars collection and CommandBar object in a Microsoft Outlook 98 solution. Common issues and examples are also provided as a starting point for working with CommandBars.

Topics covered include:
 * Overview
 * Understanding Explorers and Inspectors
 * VBScript Syntax for Using Command Bars
 * VBScript Example to Execute an Outlook Command

NOTE: The CommandBars collection is provided by the "Microsoft Office 8.0 Object Library."



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft Support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

Overview
In Microsoft Office 97, menus and toolbars are combined into one collection called CommandBars. The CommandBars collection is a shared Office component and is not directly part of the Outlook 98 object model.

Microsoft Office applications such as Microsoft Word or Microsoft Excel support Visual Basic for Applications code that runs at the "application level." This means that the object models and programming environments in those applications are designed to allow a developer to create a programming solution that encompasses the functionality of the entire application. On the other hand, Visual Basic Scripting Edition (VBScript) code in Outlook 98 is stored and runs within individual forms or items. Outlook 98 does not support the concept of application-level Visual Basic code.

NOTE: You can use the C/C++ programming language to develop a Microsoft Exchange Extension that is compatible with Microsoft Outlook 98. This is the only type of custom application-level code that Outlook supports.

For more information on creating Exchange Extensions for use with Microsoft Outlook 98, see the whitepaper "Microsoft Outlook and Exchange Extensions" available on the Microsoft Developer Network Library:

http://premium.microsoft.com/msdn/library

While Outlook 98 allows you to customize CommandBars through the user interface and by using the CommandBars objects, you cannot have a menu option or toolbar button at the application level that runs custom VBScript code. Since the Outlook 98 object model is focused on items and VBScript code within forms at the "item level," there is no direct association between an application-level toolbar or menu command and the VBScript code that runs at the item level. Note that you also cannot programmatically assign a built-in Outlook command to a custom menu or toolbar.

You can use VBScript code within an item to modify the CommandBars of the item itself. However, Microsoft does not recommend this approach since it causes two undesirable side effects:
 * If you programmatically add a button to an item, the button will also appear on other items, even those of differing types.
 * Toolbars and buttons tend to persist even after you delete them. They typically reappear after Outlook is restarted.

If you wish to provide a button the user can click to perform an action, Microsoft recommends that you place a Command Button control on the form instead of customizing an item's toolbars or menus.

The CommandBars objects can be used, however, to execute an Outlook command that is available on the toolbars or menus. Typically these commands are application-level in nature and therefore not available in Outlook's object model. For example, you can execute the Tools Synchronize command to force Outlook to synchronize folders. Note, however, that menu commands often change with newer versions of the appication, so using this approach in a solution greatly increases the chances that your solution may not function correctly in the next version of Outlook.

Understanding Explorers and Inspectors
From a developer's point of view, the main window in Outlook (the "application window") is referred to as "Explorer" and the form windows are referred to as "inspectors." Both types of windows have command bars (including both menus and toolbars). When working with the Outlook command bars, it is always important to make sure you are working with the correct set of command bars.  Use the Explorer objects (ActiveExplorer/GetExplorer) to refer to the main application window in Outlook.  Use the Inspector objects (ActiveInspector/GetInspector) to refer to an Outlook item's window.                  To reference              To create a   Window type    an open window, use:      new window, use: Application   ActiveExplorer method     GetExplorer method Item/Form     ActiveInspector method    GetInspector method 

Syntax for Referencing a Command Bar
Use the following simplified syntax in both Microsoft Visual Basic for Applications and Microsoft Visual Basic Scripting Edition (VBScript): Set MyCB = .CommandBars.Item("Menu Bar") The following syntax may not work in all cases: Set MyCB = .CommandBars("Menu Bar")

VBScript Example to Execute an Outlook Command
\You can use the following sample code with a Command Button on a contact form so that you can easily create a new letter for a contact: Sub CommandButton1_Click Set MyCB = Item.GetInspector.CommandBars.Item("Menu Bar") Set MyMenu = MyCB.Controls("Actions") Set MyMenuItem = MyMenu.Controls("New Letter to Contact") MyMenuItem.Execute End Sub

