OL98: How to Use Command Bars in Outlook Solutions

ID: Q182394

The information in this article applies to:

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:

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://www.microsoft.com/support/supportnet/refguide/default.asp

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:

appear on other items, even those of differing types. 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.

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 = <Inspector object>.CommandBars.Item("Menu Bar")

The following syntax may not work in all cases:

   Set MyCB = <Inspector object>.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

REFERENCES

For more information about using command bars in Outlook 98 solutions, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q184791
   TITLE     : OL98: How to Programmatically Synchronize Folders

   ARTICLE-ID: Q186471
   TITLE     : OL98: How to Programmatically Change Displayed Calendar Date

For more information about creating solutions with Microsoft Outlook 98, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q180826
   TITLE     : OL98: Resources for Custom Forms and Programming

   ARTICLE-ID: Q182349
   TITLE     : OL98: Questions About Custom Forms and Outlook Solutions

Additional query words: OutSol OutSol98

Keywords          : kbdta kbdtacode OffVBA 
Version           : WINDOWS:98
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: May 17, 1999