OL97: How to Use Command Bars in Outlook Solutions

ID: Q173604

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 97 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 Office 97, menus and toolbars are combined into one collection called CommandBars. The CommandBars collection is included in the Office 97 object model, making them fully programmable.

However, unlike other Microsoft Office applications, Outlook 97 command bars cannot be modified through the user interface. In addition, the Outlook object model does not support many of the options that are available through command bars in either the application or forms windows. While you can programmatically create a custom toolbar, there is no way to assign either standard Outlook commands or custom functions to the buttons. Also, Outlook does not provide a means to store custom toolbars, so any programmatic changes will be lost when you exit Outlook.

However, because the Office CommandBars collection is available through the Outlook object model, you can execute existing Outlook commands or display particular dialog boxes.

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

The following simplified syntax will always work in both Microsoft Visual Basic for Applications and Microsoft Visual Basic Scripting Edition (VBScript):

   Set MyCB = <Inspector object>.CommandBars.Item("Menu Bar")

The following simplified syntax will work using Automation if the Inspector object is declared as type Inspector, but this will fail in VBScript:

   Set MyCB = <Inspector object>.CommandBars("Menu Bar")

For example, this Visual Basic for Applications subroutine will not generate an error:

   Sub TestCommandBars()
      Dim ol As New Outlook.Application
      Dim myInspector As Inspector
      Set myInspector = ol.ActiveInspector
      Set MyCB = myInspector.CommandBars("Menu Bar")
   End Sub

However, the following sample code will generate the "Wrong number of arguments or invalid property assignment" error message:

   Sub TestCommandBars()
      Dim ol As New Outlook.Application
      Dim myInspector As Object
      Set myInspector = ol.ActiveInspector
      Set MyCB = myInspector.CommandBars("Menu Bar")
   End Sub

Using the FindControl Method

You can use the FindControl method to reference any Outlook commands (or controls, in relation to the CommandBars object model) if you know the internal value Outlook has assigned for each command. Using the internal ID means that you can use more concise syntax when executing CommandBar commands.

For example, consider a custom Outlook form that uses a custom command button to print the form. Assuming the command button is named cmdPrint, the following VBScript code examples will display the File Print dialog box:

   Sub cmdPrint_Click()
      Item.GetInspector.CommandBars.FindControl(,4).Execute
   End Sub

   Sub cmdPrint_Click()
      Item.GetInspector.CommandBars.Item("Menu Bar").Controls("File") _
         .Controls("Print...").Execute
   End Sub

Outlook's command IDs can be obtained from the CommandBars collection by using the following sample Automation code. This code must be run from Microsoft Word 97 since it will create a new document and insert a tab- delimited listing of the information in the document.

   Sub GetIDsForInspector()

      ' The Outlook object library must be referenced.
      Dim objOL As New Outlook.Application
      Dim objCommand As Object
      Set cb = objOL.ActiveExplorer.CommandBars

      ' Create a new Word document.
      Documents.Add
      With Selection
         ' 3500 is the maximum # of Controls Outlook has defined.
         For I = 1 To 3500
            Set lbl = cb.FindControl(, I)
            If lbl Is Nothing Then
               ' Do nothing.
            Else
               ' Insert CommandBar name, Command name, and ID.
               .TypeText lbl.Parent.Name & Chr(9) _
                  & lbl.Caption & Chr(9) & I
               .TypeParagraph
            End If
         Next
      End With

   End Sub

The following list of IDs was generated from running the code above on each of the Outlook item types and the application window (Explorer). The data was reformatted into the table format below.

NOTE: Because many of the Outlook windows, or inspectors, contain the same controls, duplicate controls were removed to consolidate the data. If you need to determine exactly which commands are available for each type of Outlook item or the application window, you can use the code example above to create a listing for that particular item.

   CommandBar             Control                          ID
   -----------------------------------------------------------
   Align                  Bottom                          3073
   Align                  Center                          3074
   Align                  Left                            3070
   Align                  Middle                          3075
   Align                  Right                           3071
   Align                  To Grid                         3142
   Align                  Top                             3072
   Appointment            Cancel Invitation               2487
   Appointment            Forward                          356
   Appointment            New Appointment                 1992
   Appointment            New Event                       1909
   Appointment            New Meeting Request             2777
   Arrange Buttons        Bottom                          1655
   Arrange Buttons        Right                           1656
   Center In Form         Horizontally                    1644
   Center In Form         Vertically                      1645
   Compose                Choose Form...                  1910
   Compose                Choose Template...              3176
   Compose                Forward                          356
   Compose                New Mail Message                1995
   Compose                New Mail Message                2986
   Compose                New Post in This Folder         1972
   Compose                New Post in This Folder         2007
   Compose                Post Reply to This Folder       1985
   Compose                Reply                            354
   Compose                Reply to All                     355
   Contact                Forward                          356
   Contact                New Contact                     1993
   Contact                New Contact from Same Company   1908
   Contact                New Letter to Contact           2498
   Contact                New Task for Contact            1979
   Contact                Plan a Meeting...               1084
   Context Menu           Categories...                   1888
   Context Menu           Mark as Unread                  1675
   Context Menu           Move to Folder...               1679
   Context Menu           New Contact from Same Company   1908
   Context Menu           Open                             746
   Context Menu           Print                           2521
   Dial Phone             New Call...                     2501
   Edit                   Categories...                   1888
   Edit                   Clear                             47
   Edit                   Copy                              19
   Edit                   Copy to Folder...               1676
   Edit                   Cut                               21
   Edit                   Links...                         759
   Edit                   Mark All as Read                1906
   Edit                   Mark as Read                    1674
   Edit                   Mark as Unread                  1675
   Edit                   Paste                             22
   Edit                   Paste Special...                 755
   Edit                   Revise Contents                 3273
   Edit                   Select All                       756
   Edit                   Undo                             128
   File                   Add to Outlook Bar...           1969
   File                   Archive...                      1886
   File                   Close                           2011
   File                   Close All Items                  722
   File                   Copy to Folder...               1676
   File                   Delete                           478
   File                   Exit                             752
   File                   Exit and Log Off                1891
   File                   Import and Export...            2577
   File                   Move to Folder...               1679
   File                   Open                            2779
   File                   Post                            2988
   File                   Print Preview                    109
   File                   Print...                           4
   File                   Properties                       750
   File                   Save                               3
   File                   Save and New in Company         1915
   File                   Save As...                       748
   File                   Save Attachments...             3167
   File                   Send                            3037
   Find All               Messages from Sender...         2607
   Find All               Related Messages...             2684
   Folder                 Copy "Inbox"...                 2499
   Folder                 Create Subfolder...             1660
   Folder                 Delete "Inbox"                  2500
   Folder                 Move "Inbox"...                 2506
   Folder                 Properties for "Inbox"          2502
   Folder                 Rename "Inbox"...               2512
   Form                   Advanced Properties             3166
   Form                   Display This Page               1971
   Form                   Rename Page...                  1879
   Form                   Separate Read Layout            1920
   Form                   Set Grid Size...                2686
   Form Design            Align Center                     668
   Form Design            Align Left                       664
   Form Design            Bring to Front                   166
   Form Design            Control Toolbox                  548
   Form Design            Edit Compose Page               2514
   Form Design            Edit Read Page                  2516
   Form Design            Field Chooser                   1748
   Form Design            Group                            164
   Form Design            Left                            3070
   Form Design            Properties                       222
   Form Design            Publish Form As...              1913
   Form Design            Send to Back                     167
   Form Design            Snap to Grid                    2993
   Form Design            Ungroup                          165
   Form Design            View Code                       1561
   Form Design            Width                           1646
   Go                     Calendar                        3128
   Go                     Contacts                        3129
   Go                     Deleted Items                   3134
   Go                     Go to Folder...                 1868
   Go                     Inbox                           3127
   Go                     Journal                         3132
   Go                     My Computer                     3135
   Go                     Notes                           3131
   Go                     Outbox                          3171
   Go                     Sent Items                      3133
   Go                     Tasks                           3130
   Help                   About Microsoft Outlook          927
   Help                   About this Form                 2503
   Help                   Contents and Index               983
   Help                   Microsoft Outlook Help           984
   Help                   What's This?                     124
   Horizontal Spacing     Decrease                        1649
   Horizontal Spacing     Increase                        1650
   Horizontal Spacing     Make Equal                      3137
   Horizontal Spacing     Remove                          1651
   Insert                 File...                          777
   Insert                 Item...                         2505
   Journal                Forward                          356
   Journal                New Journal Entry               1994
   Journal                Post Reply to This Folder       1985
   Layout                 AutoLayout                      2984
   Layout                 Show Grid                       2991
   Layout                 Size to Fit                      551
   Layout                 Size to Grid                    2992
   Layout                 Tab Order...                     469
   Microsoft on the Web   Best of the Web                 3031
   Microsoft on the Web   Free Stuff                      3021
   Microsoft on the Web   Frequently Asked Questions      3023
   Microsoft on the Web   Microsoft Home Page             3091
   Microsoft on the Web   Microsoft Office Home Page      3029
   Microsoft on the Web   Online Support                  3024
   Microsoft on the Web   Product News                    3022
   Microsoft on the Web   Search the Web...               3032
   Microsoft on the Web   Send Feedback...                3030
   Microsoft on the Web   Web Help 14                     3034
   Microsoft on the Web   Web Help 15                     3035
   Microsoft on the Web   Web Help 16                     3036
   Microsoft on the Web   Web Help 5                      3025
   Microsoft on the Web   Web Help 6                      3026
   Microsoft on the Web   Web Help 7                      3027
   Microsoft on the Web   Web Help 8                      3028
   Microsoft on the Web   Web Tutorial                    3033
   New Item               Appointment                     1106
   New Item               Choose Form...                  1910
   New Item               Choose Template...              3176
   New Item               Contact                         1099
   New Item               Folder...                       1755
   New Item               Journal Entry                   1989
   New Item               Mail Message                    1757
   New Item               Meeting Request                 1754
   New Item               Note                            1758
   New Item               Office Document                 2576
   New Item               Post in This Folder             2687
   New Item               Task                            1100
   New Item               Task Request                    2006
   Next                   Flagged Message                 1999
   Next                   High Importance Item            1871
   Next                   Incomplete Task                 1899
   Next                   Item                             360
   Next                   Item from Sender                1901
   Next                   Item in Conversation Topic      1903
   Next                   Last Item in Folder             1991
   Next                   Unread Item                     2000
   Open Shared Folder     Exchange Server Folder...       1863
   Open Shared Folder     Personal Folder...              1876
   Order                  Bring Forward                    170
   Order                  Send Backward                    171
   Outline                Collapse All                    1917
   Outline                Collapse This Group             1870
   Outline                Expand All                      1916
   Outline                Expand This Group               1884
   Page Setup             Define Print Styles...          1894
   Page Setup             Memo Style                       746
   Previous               First Item in Folder            1865
   Previous               Flagged Message                 1998
   Previous               High Importance Item            1872
   Previous               Incomplete Task                 1900
   Previous               Item                             359
   Previous               Item from Sender                1902
   Previous               Item in Conversation Topic      1904
   Previous               Unread Item                     2001
   Previous               Unread Item                     2001
   Previous               Unread Item                     2001
   Previous               Unread Item                     2001
   Previous               Unread Item                     2001
   Redial                 None                             746
   Remote                 Close                            923
   Remote                 Connect...                      1102
   Remote                 Disconnect                      1103
   Remote                 Mark to Retrieve                1104
   Remote                 Mark to Retrieve a Copy         1753
   Remote                 Remote Tools                    1101
   Remote                 Unmark                          1921
   Remote                 Unmark All                      1105
   Size To                Both                            1648
   Size To                Height                          1647
   Standard               Address Book...                  353
   Standard               Assign Task                     1976
   Standard               AutoDialer                       568
   Standard               AutoPreview                     1744
   Standard               Back                            1017
   Standard               Check Names                      361
   Standard               Contact                         1099
   Standard               Copy                              19
   Standard               Current View                    1893
   Standard               Cut                               21
   Standard               Day                             1094
   Standard               Delete                           478
   Standard               Digitally Sign Message           719
   Standard               Document Map                    1714
   Standard               Edit Series                     2010
   Standard               Explore Web Page                2497
   Standard               File...                         1079
   Standard               Find Items...                   2683
   Standard               Folder List                     1077
   Standard               Forward                          356
   Standard               Forward                         1018
   Standard               Go to Today                     1098
   Standard               Group By Box                    1750
   Standard               Importance: High                1074
   Standard               Importance: Low                 1075
   Standard               Invite Attendees                1086
   Standard               Item                             359
   Standard               Item                             360
   Standard               Mark Complete                   2985
   Standard               Message Flag...                 1678
   Standard               Message Header                  1751
   Standard               Microsoft Outlook Help           984
   Standard               Month                           1096
   Standard               Move to Folder                  2778
   Standard               New Meeting with Contact        1980
   Standard               New Message to Contact          1978
   Standard               Next Item                       2614
   Standard               Paste                             22
   Standard               Plan a Meeting...               1084
   Standard               Previous Item                   2615
   Standard               Print                           2521
   Standard               Print Preview                    109
   Standard               Print...                           4
   Standard               Recurrence...                   1977
   Standard               Reply                            354
   Standard               Reply to All                     355
   Standard               Save                               3
   Standard               Save and Close                  1975
   Standard               Save and New                    1665
   Standard               Seal Message with Encryption     718
   Standard               Send                            2617
   Standard               Send Status Report              1760
   Standard               Undo                             128
   Standard               Up One Level                    1762
   Standard               Week                            1095
   Task                   Cancel Assignment               2004
   Task                   Forward                          356
   Task                   New Task                        1997
   Task                   New Task Request                1761
   Task                   Reply                            354
   Task                   Reply to All                     355
   Task                   Skip Occurrence                 1919
   Tasks                  Choose Form...                  1910
   Tasks                  Choose Template...              3176
   Tasks                  New Task                        1997
   Tasks                  New Task Request                1761
   Tasks                  Post Reply to This Folder       1985
   Tasks                  Save Task Order                 1912
   Toolbar List           Standard                         761
   Tools                  Address Book...                  353
   Tools                  AutoCorrect...                   793
   Tools                  AutoSignature...                1078
   Tools                  Check for New Mail              1677
   Tools                  Check for New Mail On...        1890
   Tools                  Check Names                      361
   Tools                  Design Outlook Form             2519
   Tools                  Empty "Deleted Items" Folder    1671
   Tools                  Find Items...                   2683
   Tools                  Letter Wizard...                 796
   Tools                  Options...                       522
   Tools                  Protect Document                 336
   Tools                  Recall This Message...          2511
   Tools                  Record in Journal               1672
   Tools                  Resend This Message...          3165
   Tools                  Services...                     1882
   Tools                  Spelling...                        2
   Vertical Spacing       Decrease                        1652
   Vertical Spacing       Increase                        1653
   Vertical Spacing       Make Equal                      3138
   Vertical Spacing       Remove                          1654
   View                   Bcc Field                       1860
   View                   Calendar...                     1987
   View                   Define Views...                 1862
   View                   Field Chooser                   1748
   View                   Filter...                       1752
   View                   Format Columns...               1898
   View                   Format View...                  1875
   View                   From Field                      1867
   View                   Group By...                     1666
   View                   Master Document                 2190
   View                   Outlook Bar                     2515
   View                   Show Fields...                  2009
   View                   Sort...                         2916
   View                   Status Bar                       850

REFERENCES

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

   Article-ID: Q166368
   Title     : OL97: How to Get Help Programming with Outlook

   Article-ID: Q170783
   Title     : OL97: Q&A: Questions about Customizing or
               Programming Outlook

Additional query words: OutSol OutSol97
Keywords          :  
Version           : WINDOWS:97
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: May 17, 1999