C H A P T E R    8 Microsoft Office 97/Visual Basic Programmer's Guide

Menus and Toolbars


Contents

An essential part of creating a useful custom application is providing a simple and consistent way for the user to interact with your Visual Basic application. Menus and toolbars provide a quick, convenient, and widely accessible way to expose simple commands and options to the user. In Microsoft Office 97, menus and toolbars are easy to design and modify; Microsoft Access 97, Microsoft Excel 97, Microsoft Word 97, and Microsoft PowerPoint 97 all share the same basic customization interface — the Customize dialog box. Because all menus and toolbars are represented by the same type of object — the CommandBar object — they're easy to customize and control from Visual Basic, as well.

The information in this chapter covers the shared menu and toolbar customization features of Microsoft Access, Microsoft Excel, Microsoft Word, and Microsoft PowerPoint. For more information about customizing menus and toolbars in Microsoft Access, see Chapter 1 in Building Applications with Microsoft Access 97, which is available in Microsoft Access 97 and Microsoft Office 97, Developer Edition. An online version of that book is available in the Value Pack on CD­ROM in Microsoft Access 97 and Microsoft Office 97, Professional Edition.

Note   Microsoft Outlook doesn't provide an interface for customizing menus and toolbars. Therefore, none of the information in this chapter about the Customize dialog box applies to Microsoft Outlook.

Tools for Modifying the User Interface

There are two tools for customizing menu bars and toolbars: the shared Customize dialog box and Visual Basic. Although the Customize dialog box differs slightly from one Office application to the next, the programmable objects used to modify menu bars and toolbars are the same across all applications. This section describes the Customize dialog box and the shared programmable objects, as well as when and how to use these tools.

The Customize Dialog Box

The Office applications (excluding Outlook) provide a common interface — the Customize dialog box — for making design­time changes to your Visual Basic application. Design­time changes to menu bars and toolbars are any changes you make before the application runs. This includes adding, deleting, moving, and restoring menu components and toolbar controls, as well as setting menu­component and toolbar­control properties that won't change in response to changing conditions at run time.

The sections in this chapter discuss how to modify menu bars and toolbars by using either the Customize dialog box or Visual Basic code. In cases where you can use either technique to make the same modifications, using the Customize dialog box to make design­time changes is quicker and easier. Therefore, you should be familiar with the elements of and techniques for using this dialog box.

To display the Customize dialog box

  • On the View menu, point to Toolbars, and then click Customize.

    The following illustration shows the Toolbars tab in the Customize dialog box displayed by PowerPoint.

    Microsoft Access, Microsoft Excel, and Microsoft Word all provide the same controls in the Customize dialog box (on the Toolbars, Commands, and Options tabs) as does PowerPoint, but these first three applications also include other elements on the Toolbars and Commands tabs that are specific to customizing those applications. Those elements are described in the following paragraphs.

    Microsoft Access   The Toolbars tab contains a Properties button that displays the Toolbar Properties dialog box. Use this dialog box to set properties of built­in or custom menu bars and toolbars. For more information about using the Toolbar Properties dialog box, see Chapter 1 in Building Applications with Microsoft Access 97.

    Microsoft Excel   The Toolbars tab contains an Attach button that displays the Attach Toolbars dialog box. You can use this dialog box to copy menu bars and toolbars from the application workspace to the active workbook. For more information, see "Scope of Changes to the User Interface" later in this chapter.

    Microsoft Word   The Commands tab contains a Save in box you can use to specify the context of the design­time changes you make in the Customize dialog box. The New Toolbar dialog box that appears when you click the New button on the Toolbars tab contains the Make toolbar available to box. For more information, see "Scope of Changes to the User Interface" later in this chapter. The Commands tab also contains a Keyboard button that displays the Customize Keyboard dialog box; you can use this dialog box to assign shortcut keys to any macro or built­in Word command.

    After you've opened the Customize dialog box in any Microsoft Office application, you follow the same general procedure for modifying any built­in or custom menu or toolbar, as described by the following steps.

    1. In the Toolbars box on the Toolbars tab, select the check box next to the name of the menu bar or toolbar you want to display and modify. When you create a new menu bar or toolbar, it's automatically displayed.

    2. Click any menu item (including menu and submenu captions) or toolbar control to select it. The command associated with the control doesn't run while the Customize dialog box is open.

    3. Right­click the item or control you've selected to display the shortcut menu containing the available customization options. Options for menu commands and toolbar buttons include resetting the command; deleting the item or control; changing its name; specifying whether it should have a name, an image, or both displayed; modifying its image; and setting it to begin a group (that is, to appear with a line above or before it).

    Note   One or more of these options may not be available for built­in commands or controls; unavailable options appear dimmed on the shortcut menu.

    While the Customize dialog box is open, you can rearrange items and controls by dragging and dropping them, and you can add new items and controls from the Commands tab. For more information about adding new items and controls, see the corresponding procedures in "Design­Time Modifications to the Menu System" and "Design­Time Modifications to Toolbars" later in this chapter.

    Visual Basic

    In general, to create or modify the user interface of the Microsoft Office application in which you're delivering your Visual Basic application, you should use the Customize dialog box. Changes you make to the user interface by using the Customize dialog box are known as design­time changes.

    You can also add to and modify menus and toolbars by using the command bar portion of the shared Microsoft Office object model in Visual Basic code: the top­level object is the CommandBars collection, which is returned by the CommandBars property in all the Microsoft Office applications. Every menu bar, shortcut menu, and toolbar is represented by a CommandBar object in this collection. Every CommandBar object contains a CommandBarControls collection; each control on a menu bar or toolbar is represented by a member of this collection.

    For more information about the CommandBars collection, all the objects it contains, and the properties and methods of those objects, see "Overview of command bars" and the corresponding object, property, and method topics in Help.

    You can write code that runs just once to create or change elements of menus or toolbars; in effect, the code simulates making design­time changes in the Customize dialog box. In some Microsoft Office applications, however, you may be required to use a combination of this kind of Visual Basic code and the Customize dialog box to design your Visual Basic application. The following are some common areas where you must use a combination of code and the container application's interface:

    You can also write code that exists in your Visual Basic application to make changes to the menu system while your application is running (for example, you can write code to disable a command on a menu under certain conditions, or to add a menu to a menu bar in response to a user's actions). Changes brought about by your code while your Visual Basic application is running are known as run­time changes.

  • Scope of Changes to the User Interface

    Each Microsoft Office application uses slightly different rules regarding where and how changes to the user interface are stored. It's important to understand how you can control the scope of the changes, because the ability of your Visual Basic application to display your custom interface correctly depends on it.

    Microsoft Access

    The following information describes managing and storing menu bars and toolbars in Microsoft Access. For more information about working with menu bars and toolbars in Microsoft Access, see Chapter 1 in Building Applications with Microsoft Access 97.

    You can use custom menu bars and shortcut menus in your custom application in three ways:

    • Attached to a form or report. Microsoft Access displays your custom menu bar whenever you open the form or display the report in print preview. For more information, see "MenuBar Property" in Help.

    • As a shortcut menu attached to a form, a control on a form, or a report. Microsoft Access displays your custom menu whenever you right­click the form, control, or report it's attached to. For more information, see "ShortCutMenuBar Property" in Help.

    • As your application's global menu bar. Microsoft Access displays your custom menu bar in all windows, except in forms or reports that have their own custom menu bar. (A form or report's custom menu bar overrides a global custom menu bar.) You can specify a menu bar to use throughout your application by using the Startup dialog box.

    You can use one or more custom toolbars in an application. Create the toolbars you want, and then use the appropriate method to display your custom toolbars:

    • If your application has only one custom toolbar, just use the Toolbars command (View menu) to display it; it will appear each time your application starts.

    • If your application has different custom toolbars for different forms or reports, you can specify a toolbar for each form or report in the form or report's Toolbar property.

      Note   There is no need to create event procedures for the Activate and Deactivate events of the form to show and hide toolbars, as was required in previous versions of Microsoft Access. Setting the Toolbar property to a custom toolbar automatically hides the built­in Form View toolbar when your form is opened and hides your custom toolbar when a user closes the form or switches to another form.

    • If you need to work with more than one custom toolbar for a form or report, or if you want to hide or show built-in Microsoft Access toolbars, you can use the Visible property of the CommandBar object in Visual Basic code or use the ShowToolbar action in macros to hide and show the toolbars.

    • If you want your application to display only custom toolbars, you can hide all built-in toolbars by clicking the Startup command (Tools menu) and then clearing the Allow Built-in Toolbars check box.

    Microsoft Excel

    You can store custom menu bars and toolbars with the workspace or with the workbook. When you quit Microsoft Excel, the toolbars in the workspace are automatically saved in the file Username8.xlb (where Username is the current user's Windows 95 logon name). If the user isn't logged on, the file name is Excel8.xlb. The toolbars saved in a workbook are stored in the workbook file.

    Workbook­level menu bars and toolbars make it easier for you to create a polished user interface for a custom application (an add­in, for instance) and to distribute custom toolbar buttons and their supporting procedures. If you're going to distribute a custom toolbar with a custom application, you should attach it to the workbook that contains that application so that the toolbar is stored in the same file as the application.

    To move a menu bar or toolbar from the workspace to a workbook

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. On the Toolbars tab, click Attach.

      The Attach Toolbars dialog box is displayed.

    3. In the Custom toolbars box, click the name of the menu bar or toolbar you want to copy to the active workbook.

    4. Click Copy.

      The name of the menu bar or toolbar you copied appears in the Toolbars in workbook box.

    You can delete the original workspace­level menu bar or toolbar by clicking the Toolbars tab in the Customize dialog box, selecting the name of the menu bar or toolbar you want to delete, and then clicking Delete. If you don't delete the workspace version of the menu bar or toolbar, you can change it without affecting the version stored in the workbook. If you make changes to the workspace version of the menu bar or toolbar and would like to update the workbook version to match the current workspace version, you can copy the workspace version to the workbook again, thus replacing the previous workbook version.

    After you've copied a menu bar or toolbar to a workbook, the menu bar or toolbar becomes available only after the user has opened that workbook. A workbook version of the menu bar or toolbar retains not only its name and contents, but also the code assignments for menu items or toolbar controls; the location, size, and shape of the menu bar or toolbar; its on­screen position; and whether it's visible or hidden.

    You can also delete a workbook version of a menu bar or toolbar.

    To delete a workbook version of a menu bar or toolbar

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. On the Toolbars tab, click Attach.

      The Attach Toolbars dialog box is displayed.

    3. In the Toolbars in workbook box, click the name of the menu bar or toolbar you want to delete.

    4. Click Delete.

    Note   You cannot use Visual Basic to attach menu bars or toolbars to a workbook or delete them from a workbook.

    When you open a workbook that contains one or more menu bars or toolbars, Microsoft Excel first determines whether a workspace menu bar or toolbar with that name already exists. If not, Microsoft Excel creates a new workspace menu bar or toolbar and copies the workbook version into it. This way, the you get a fresh copy of the menu bar or toolbar, which you can alter by hiding it or by copying items or controls to or from the workspace­level copy. When you quit Microsoft Excel, changes made to this copy of the menu bar or toolbar are stored with the workspace file.

    There's no way to rename a menu bar or toolbar, so when the workbook is reopened, the workspace already contains a menu bar or toolbar with the same name as the workbook version, and Microsoft Excel uses the workspace copy rather than reloading the workbook version. However, the procedures that support the menu items or toolbar buttons in the open workbook still run when the user clicks the corresponding item or control.

    As a developer, you can design a menu bar or toolbar and then attach it to a workbook. When the user opens the workbook, the custom menu bar or toolbar is available. The user can then edit it and move items or controls from it to personal menu bars or toolbars, without affecting the copy stored in the workbook. The user's changed menu bars and toolbars are stored with the workspace file when he or she quits Microsoft Excel. When the user starts Microsoft Excel again, the edited menu bar or toolbar is available; clicking one of the developer's menu items or toolbar controls loads the workbook that contains the procedure attached to that item or control. To generate a fresh copy of the workbook menu bar or toolbar, the user can delete the edited copy.

    Microsoft Word

    Word stores custom menus and toolbars in templates, just as it does with macros. When you customize a menu or create a new toolbar, changes are stored by default in the Normal template and are available "globally" — that is, you can always display a custom toolbar stored in the Normal template, even if the active document is based on a different template. A toolbar stored in a template other than Normal is available under either of two circumstances: the template is attached to the active document, or the template is loaded as a global template (Tools menu, Templates and Add­ins command). When you store a toolbar in a document, you can display the toolbar only when the document itself is active.

    If you're going to distribute a Visual Basic application with customized menus and toolbars, you should store your menu bars and toolbars in a custom template or in a document. Because every user has his or her own Normal template, your Visual Basic application shouldn't change the Normal template. It's also easier to remove custom menus and toolbars when the user quits your application if the customizations are in the template or document that contains your application. That is, when the user closes the document (if the document contains toolbars or the template it's attached to contains toolbars) or unloads your template, your custom toolbars are no longer available; only the built­in menus and toolbars or the user's custom toolbars remain.

    If two custom toolbars with the same name are available at the same time (for example, if the Normal template and a loaded global template both have a toolbar named "Custom Tools"), both toolbars are listed in the Customize dialog box and can be displayed either separately or at the same time.

    In Visual Basic, you can add, customize, or delete menu bars and toolbars in any document or template. However, because the CommandBars property applies only to the Application object, you must set the context for your change before you make the change. Similar to using the Store in box on the Commands tab in the Customize dialog box, you can use the CustomizationContext property in Visual Basic to specify a Document or Template object that represents the document or template in which you want to make changes. You must set the CustomizationContext property before using the CommandBars property; this ensures that a reference to the collection of menu bars and toolbars for that document or template is returned. For more information, see "Design­Time Modifications to Toolbars" later in this chapter.

    Microsoft PowerPoint

    Custom menu bars and toolbars are always stored with the workspace. When you quit PowerPoint, the toolbars in the workspace are saved in the file Username.pcb (where Username is the current user's Windows 95 logon name). If the user isn't logged on, the file name is Powerpnt.pcb.

    Because menu bars and toolbars aren't visible while a presentation is running, customizing menus and toolbars in PowerPoint is limited to changing the available menu commands and toolbar controls in design mode. You can use either the Customize dialog box or Visual Basic to modify your own design environment. If your Visual Basic application delivers a custom interface for designing presentations, you must use Visual Basic to make changes to menus and toolbars. When the user finishes with your application, it's a good idea for you to remove changes you made in Visual Basic.

    Choosing the Best User­Interface Enhancement

    Menus are lists of user­interface commands from which the user can choose. Menus offer a convenient and consistent way to group commands and an easy way for users to get to them. Commands for performing related tasks can be listed on the same menu, and commands can also be grouped (separated by lines from other commands or groups of commands). Submenus offer additional levels of organization, and shortcut menus offer a way to group related commands that apply to the limited context of a specific task.

    You can assign access keys to make commands accessible from the keyboard, and you can assign shortcut keys to provide the user even quicker access to the commands. In addition, menus take up less space than toolbars, as the items on a menu are displayed on demand and don't take up dedicated screen space. On the other hand, if you want quick, graphical access to a command, a toolbar may be a better choice.

    Toolbars contain controls that perform frequently used commands. Toolbars are ideal for presenting individual property settings (such as bold or italic formatting, or font size), commands that are best represented visually, and commands you want to access with one click of the mouse. In addition, toolbars remain displayed while the user works, whereas menus are displayed only on demand; this makes scanning a toolbar for a particular button easier than scanning the menus on a menu bar for a particular command. However, if you need easy keyboard access to a command, if you want to display your commands hierarchically, or if you are short on screen space, a menu may be a better choice.

    If you need to present a more complex set of options to the user, a dialog box may be a better choice than either a toolbar or a menu. If you want to place a tool closer to the data the user is working with, the best solution may be to place a control directly on a worksheet or document. For more information about these various types of user­interface enhancements, see Chapter 12, "ActiveX Controls and Dialog Boxes."

    The Menu System

    The menu system in each Microsoft Office application is composed of the entire set of menus and the items on each menu. Each menu is either a menu, a submenu, or a shortcut menu. Each menu item is usually either a command or a submenu caption. In this chapter, the term component refers generically to any menu or menu item.

    A menu bar is a bar at the top of the active window that displays the names of all the menus that are available in that application at any given time. That is, a Microsoft Office application can change the menu bar it displays in response to a change in the active window or in response to a Visual Basic instruction. For example, when you edit a chart in Microsoft Excel, the menu bar containing a set of menus that apply to the charting environment is automatically displayed.

    A menu is a list of menu items that appears (drops down) when you click a menu name on the menu bar.

    A submenu (or child menu) is a menu that's attached to the side of another menu (the parent menu), adjacent to a particular submenu caption on the parent menu. Each submenu caption is marked with an arrowhead pointing to the right. You can add submenus to menus or shortcut menus. A submenu is displayed when you point to the corresponding submenu caption on the parent menu.

    A shortcut menu is a floating menu that contains a group of commands pertinent to a specific task. A shortcut menu appears when the user right­clicks an object.

    Guidelines for Customizing the Menu System

    You can modify the menu system in a Microsoft Office application in a wide range of ways: you can create new menu bars, add new menus to built­in or custom menu bars, add new menu items (commands or submenus) to built­in or custom menus or submenus, add and modify shortcut menus, and assign macros to menu items. In addition, you can restore the built­in menu system to its default state at any time.

    Adding Custom Components or Modifying Built­in Components

    Each Microsoft Office application comes with its own built­in menu system. You can modify components of this built­in system or create and modify custom menu components.

    Modifying a built­in menu bar, menu, or menu item is appropriate if you're adding or changing a small number of components. For example, if you just want to provide menu access to a macro, you can add a menu item to a built­in menu and then link the macro to that item.

    If you need to make more extensive changes, you may be better off creating a completely new component. For example, if you want to add several new menus — each of which will contain several new menu items — it may be more appropriate to create an entirely new menu bar to contain the new menus.

    Using Submenus

    If your menus become crowded and difficult to scan, you can use submenus to organize them more effectively and add clarity to your Visual Basic application by reducing the amount of information presented to the user at any one time. For example, suppose that you create a menu that presents a number of options, as shown in the following illustration.

    Using submenus, you can present the same items in either of the ways shown in the following illustration.

    With submenus, the user can browse through commands that might otherwise be available only through a series of custom dialog boxes. However, if you need to create a complicated array of submenus to present a set of commands, a dialog box may be a better solution.

    Using Shortcut Menus

    If you want to give the user access to a command that applies only to the limited context of a selected object, you can add the command to the built­in shortcut menu for that object. In Microsoft Access, you can also create custom shortcut menus and associate them with objects in your application. For information about creating and using custom shortcut menus in Access, see Chapter 1 in Building Applications with Microsoft Access 97.

    Using Text Boxes, List Boxes, and Combo Boxes

    Although it's possible to add built­in or custom text boxes, list boxes, and combo boxes to menus, shortcut menus, and submenus in your Visual Basic application, such controls are better suited to toolbars. Text boxes on menus can be useful in some instances to display or return a simple setting. List boxes and combo boxes also display the current setting when a menu is displayed. However, as soon as the user selects a new value in the box, the menu is closed (the user cannot see or revise the setting and must display the menu again to verify it).

    If you want to add built­in text boxes, list boxes, and combo boxes to menus, use the same techniques given for adding built­in commands (see "Adding and Grouping Commands" later in this chapter). If you want to add custom text boxes, list boxes, and combo boxes, use the same techniques given for adding them to toolbars (see "Design­Time Modifications to Toolbars" later in this chapter).

    Design­Time Modifications to the Menu System

    Design­time changes to the menu system are any changes you make before the application runs. These include adding, deleting, moving, and restoring menu components, as well as setting menu component properties that won't change in response to changing conditions at run time.

    Adding a Custom Menu Bar

    If you want to design a set of menus that differs significantly from what's currently available on the Office application's built­in menu bar or menu bars, you may need to create a new menu bar. You can do this by using the Customize dialog box in Microsoft Access, or by using Visual Basic in Microsoft Excel, Word, or PowerPoint.

    Using the Customize Dialog Box

    In Microsoft Access, the Customize dialog box provides a convenient way to add a custom menu bar.

    To add a menu bar in Microsoft Access

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. On the Toolbars tab, click New.

    3. In the Toolbar name box, type a name for the new menu bar, and then click OK.

      An empty, floating menu bar with the name you typed is displayed.

    4. Click Properties to display the Toolbar Properties dialog box.

    5. In the Type box, click Menu Bar.

      You can also set many other properties of your custom menu bar in the Toolbar Properties dialog box. For information about these properties and their uses, see Chapter 1 in Building Applications with Microsoft Access 97.

    The new menu bar is added to the list of in the Toolbars box on the Toolbars tab.

    Using Visual Basic

    You use the Add method of the CommandBars collection to create a new menu bar; the MenuBar argument of the Add method determines whether the CommandBar object you're creating can be displayed as a menu bar. The following example creates a new menu bar named "Custom Menu Bar."

    Set cstm = CommandBars.Add(Name:="Custom Menu Bar", Position:=msoBarTop, _
        MenuBar:=True, Temporary:=False)
    
    

    In Microsoft Excel, Word, and PowerPoint, you must use Visual Basic to create a new menu bar. In Microsoft Access, you have the option of using either Visual Basic or the Customize dialog box.

    Adding Menus

    You can add a menu to any built­in or custom menu bar. Because a Microsoft Office application can display different built­in menu bars in different contexts, you may have to add a command to more than one menu bar to make sure that the user has access to the command regardless of the context. For example, in Microsoft Excel, you might want to add a special Accounting menu to each menu bar so that employees in a company can run the corresponding macros from any sheet.

    When you add a menu to a menu bar, you can specify an access key for the menu; the access key appears underlined when the menu is displayed.

    Note   Although a Microsoft Office application may list toolbars that contain shortcut menus on the Toolbars tab in the Customize dialog box, you can neither add custom shortcut menus directly to these toolbars nor delete built­in shortcut menus from them. You can, however, add items to, delete items from, or customize items on shortcut menus. For information about customizing shortcut menus, see "Adding and Modifying Shortcut Menus" later in this section.

    Using the Customize Dialog Box

    The Customize dialog box provides a convenient way to add a menu to a built­in or custom menu bar.

    To add a custom menu to a menu bar

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. If the menu bar you want to modify isn't visible, select the check box next to the name of that menu bar in the Toolbars box on the Toolbars tab.

    3. On the Commands tab, click New Menu in the Categories box.

    4. Drag the New Menu item from the Commands box to the position on the menu bar where you want to add the menu.

      An I­beam on the menu bar indicates the position where the new menu will be added when you release the mouse button.

    5. Right­click the new menu, and then type a name in the Name box. Type an ampersand (&) before the character you want to use as the access key for the menu.

    When you click the menu name, an empty menu is displayed. For information about adding menu items to the new menu, see "Adding and Grouping Commands" later in this section.

    The Customize dialog box also provides a quick way to add a copy of any built­in menu to a built­in or custom menu bar. You can customize the commands on the copy without affecting the original built­in menu.

    To add a copy of a built­in menu to a menu bar

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. If the menu bar you want to modify isn't visible, select the check box next to the name of that menu bar in the Toolbars box on the Toolbars tab.

    3. On the Commands tab, click Built­in Menus in the Categories box.

    4. Drag a built­in menu from the Commands box to the position on the menu bar where you want to add the copy.

      An I­beam on the menu bar indicates the position where the menu will be added when you release the mouse button.

    Tip   You can also make a copy of any built­in menu by displaying the menu bar that contains that menu and holding down CTRL while you drag the menu to another menu bar.

    Using Visual Basic

    Use the Add method of the CommandBarControls collection to add a menu to a CommandBar object that represents a particular menu bar. Setting the Type argument of the Add method to msoControlPopup indicates that the control you're adding displays a menu. Controls that display menus are known as pop­up controls. The Before argument indicates the position of the new menu among the existing menus on the menu bar. Set the Caption property of the CommandBarPopup object returned by the Add method to specify the menu name and the access key. The following Microsoft Excel example adds a new menu named "Accounting" to the left of the Window menu on the menu bar for worksheets.

    Set cstmAccounting = CommandBars("Worksheet Menu Bar").Controls _
        .Add(Type:=msoControlPopup, Before:=9)
    cstmAccounting.Caption = "&Accounting"
    
    

    Note   You use an ampersand (&) in the menu name in front of the character that will be used as the access key for the menu. After you've added the menu, you can specify the menu name either with or without the ampersand when you reference the menu using Controls(index).

    Adding Submenus

    A submenu (child menu) is a menu attached to the side of another menu (the parent menu), adjacent to a particular menu item (the submenu caption). You can add submenus to menus, other submenus, and shortcut menus.

    Just as you display the items on a menu by clicking the menu name on the menu bar, you display items on a submenu by pointing to the submenu caption on the parent menu. Similarly, just as you first add an empty menu (having a name but no menu items) to a menu bar and then add individual menu items, you first add an empty submenu to a parent menu and then add menu items.

    Using the Customize Dialog Box

    You use the Customize dialog box to add a submenu to another menu.

    To add a submenu to a menu

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. If the menu bar that contains the menu you want to modify isn't visible, select the check box next to the name of that menu bar in the Toolbars box on the Toolbars tab.

    3. On the Commands tab, click New Menu in the Categories box.

    4. Drag the New Menu item from the Commands box to the position on the menu where you want to add the submenu.

      To indicate the position for the new submenu, drag it over the menu name (and subsequent submenu captions, if necessary) to open the menu or submenu you want, drag the new submenu to the location where you want it on the menu or submenu, and then release the mouse button. A horizontal I­beam on the menu indicates the position where the submenu will be added when you release the mouse button.

    5. Right­click the new submenu, and then type a name in the Name box. Type an ampersand (&) before the character you want to use as the access key for the submenu.

    When you click the submenu caption, an empty submenu is displayed. For information about adding menu items to the new submenu, see "Adding and Grouping Commands" later in this section.

    Using Visual Basic

    Use the Add method of the CommandBarControls collection to add a submenu to a CommandBar object that represents another menu. Setting the Type argument of the Add method to msoControlPopup indicates that the control you're adding is a pop­up control — the same kind of control that indicates a menu on a menu bar. The Before argument indicates the position of the new menu among the existing items on the menu. Set the Caption property of the CommandBarPopup object returned by the Add method to specify the submenu caption and the access key. The following Microsoft Excel example adds a new submenu named "Product" at the end of the Accounting menu on the menu bar for worksheets.

    Set cstmAcctProduct = CommandBars("Worksheet Menu Bar").Controls("Accounting") _
        .Controls.Add(Type:=msoControlPopup)
    cstmAccProduct.Caption = "&Product"
    
    

    Note   You use an ampersand (&) in front of the character in the submenu caption that will be used as the access key for the submenu. After you've added the submenu, you can specify the submenu name either with or without the ampersand when you reference the submenu by using Controls(index).

    Adding and Grouping Commands

    You can add commands to any built­in or custom menu or submenu, modify their appearance, and visually separate them into logical groupings. This section deals with adding commands to menus and submenus. The following section addresses the specific issues of adding shortcut menus and then adding menu items to them.

    Note   Although you can add text boxes, list boxes, and combo boxes to menus and submenus, they're not usually the best choice for presenting or returning information. If you want to add custom text boxes, list boxes, and combo boxes, use the same techniques given for adding them to toolbars (see "Design­Time Modifications to Toolbars" later in this chapter).

    Using the Customize Dialog Box

    The Customize dialog box provides an easy way to add items to menus and submenus.

    To add a built­in command to a menu or submenu

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. If the menu bar that contains the menu you want to modify isn't visible, select the check box next to the name of that menu bar in the Toolbars box on the Toolbars tab.

    3. On the Commands tab, select a category of commands in the Categories box.

      The commands in the category you select are listed in the Commands box.

    4. Drag a command from the Commands box to the position on the menu or submenu where you want to add the command.

      To indicate the position for the command, drag it over the menu name (and subsequent submenu captions, if necessary) to open the menu or submenu you want, drag the command to the location where you want it on the menu or submenu, and then release the mouse button. A horizontal I­beam on the menu indicates the position where the command will be added when you release the mouse button.

    Tip   You can also make a copy of any built­in command by displaying the menu bar that contains a menu with that command and holding down CTRL while you drag the command to another menu.

    The Customize dialog box also provides a quick way to add a custom command to a built­in or custom menu bar. However, each Microsoft Office application involves a different technique for using the Customize dialog box to do this. The following paragraphs describe these differences.

    Microsoft Access   To add a menu item that runs a macro, follow the same steps as in the procedure for adding a built­in command to a menu. In the Categories box, click All Macros. Drag the macro you want from the Commands box to the position on the menu where you want it to appear. To add a menu item that runs a Function procedure, follow the same steps as in the procedure for adding a built­in command to a menu. In the Categories box, click any category, and then drag any item you want to the position on the menu where you want it to appear. Right­click the item, and then click Control Properties to display the Control Properties dialog box. In the Caption box, delete the current name, and then type a new name for your command. In the On Action box, type an expression to run your Visual Basic Function procedure. The expression must use the following syntax: =functionname().

    Microsoft Excel   Follow the same steps as in the procedure for adding a built­in command to a menu; in the Categories box, click Macros, and then drag Custom Menu Item from the Commands box to the position on the menu where you want it to appear. Right­click the new item and then click Assign Macro. In the Macro Name box in the Assign Macro dialog box, enter the name of the macro you want to run.

    Microsoft Word   Follow the same steps as in the procedure for adding a built­in command to a menu; in the Categories box, click Macros, and then drag a macro from the Commands box to the position on the menu where you want it to appear.

    Tip   In Word, if you write a procedure whose name is the same as that of a built­in Word command (or if you write a procedure named "MAIN" in a module whose name is the same as that of a built­in Word command), that procedure will replace the built­in functionality of the command whenever the module that contains it is available. Every copy of the menu item on whatever menu it appears will run the replacement procedure. For more information about controlling the context of your customizations, see "Scope of Changes to the User Interface" earlier in this chapter. For more information about modifying Word commands, see Chapter 7, "Microsoft Word Objects."

    Microsoft PowerPoint   Follow the same steps as in the procedure for adding a built­in command to a menu; in the Categories box, click Macros, and then drag a macro from the Commands box to the position on the menu where you want it to appear.

    Modifying the Appearance of a Command

    Any command on a menu can have a button image displayed next to the command name. Whether a button image appears next to a command is determined by its "style." You set a command's style using commands on the shortcut menu while the Customize dialog box is open. The following table describes the effect of each style on menu commands.

    StyleWhat appears on a menu
    Default StyleButton image and name
    Text Only (In Menus) Name only
    Text Only (Always) Name only
    Image And TextButton image and name

    Note   By default, some built­in menu commands don't have a button image associated with them and won't display an image regardless of the style you set. However, you can add an image to any built­in menu command.

    While the Customize dialog box is open, you can add or modify the button image next to a menu command. The following table describes the techniques you can use.

    ToDo this
    Use a predefined button image Right­click the command, point to Change Button Image, and then click the image you want.
    Copy and paste another command's button image Right­click the command that has the image you want to use, and then click Copy Button Image. Right­click the command whose image you're customizing, and then click Paste Button Image.
    Copy and paste an image from a graphics program In a graphics program, open the image you want to copy. Select and copy the image (preferably a 16 x 16 pixel image or portion). Switch back to your application. Right­click the command, and then click Paste Button Image.
    Edit the command's current button image Right­click the command, and then click Edit Button Image. In the Button Editor dialog box, you can change the color and shape of the image, adjust the image's position on the control, and preview your changes to the image. When you finish editing the button image, click OK.
    Reset a command to use its original button image (or no image) Right­click the command, and then click Reset Button Image.

    Tip   In Microsoft Access, you can use the Properties dialog box to set many other properties of menu commands. For more information, see Chapter 1 in Building Applications with Microsoft Access 97.

    Grouping Commands

    You can group related commands on a menu by separating them with lines. The lines themselves aren't menu items; rather, you can set any item on a menu to appear with a line before it. You use the Customize dialog box to set a command to appear as the first item in a group of commands.

    To begin a group of commands on a menu

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. If the menu bar that contains the menu you want to modify isn't visible, select the check box next to the name of that menu bar in the Toolbars box on the Toolbars tab.

    3. Right­click the menu item you want to appear with a line above it, and then click Begin Group.

      The next time you right­click that item, a check mark will be displayed next to Begin Group on the shortcut menu. To remove the line before a menu item, right­click the item and then click Begin Group again (the check mark will no longer appear).

    Using Visual Basic

    Use the Add method of the CommandBarControls collection to add a new menu item to the CommandBar object that represents a particular menu or submenu. To add a built­in command, specify the ID number of the command by using the Id argument of the Add method. The following example adds the Spelling command to the menu named "Quick Tools" on the menu bar named "Custom Menu Bar."

    Set mySpell = CommandBars("Custom Menu Bar").Controls("Quick Tools") _
        .Controls.Add(Id:=2)
    
    

    For information about determining the built­in command ID numbers of a Microsoft Office application, see "Menu Item and Toolbar Control IDs" later in this chapter.

    To add a custom command, you add a new menu item and then set the OnAction property to specify a Visual Basic procedure to run whenever that item is clicked. Setting the Type argument of the Add method to msoControlButton indicates that a menu item is a command. The following Microsoft Excel example adds an Open Database menu item to the File menu on the menu bar for worksheets. Microsoft Excel runs the OpenDatabaseProc Visual Basic procedure whenever the user clicks this menu item. Open Database appears directly above the Close command on the File menu.

    Set databaseItem = CommandBars("Worksheet Menu Bar").Controls("File") _
        .Controls.Add(Type:=msoControlButton, Before:=3)
    With databaseItem
        .Caption:="Open Database"
        .OnAction:="OpenDatabaseProc"
    End With
    
    
    There are many properties of the objects that represent menu commands that you can set in Visual Basic to modify the appearance of commands. For more information, see "Style Property" and "FaceID Property" in Help, as well as the Help topics for other properties and methods of the CommandBarButton object.

    To set a menu item to begin a group of menu items (that is, to be preceded by a line), you just set the BeginGroup property of the CommandBarButton, CommandBarPopup, or CommandBarComboBox object that represents the menu item to True. To remove the line, set the BeginGroup property to False. Use Controls(index), where index is the caption or index number of a menu item, to return an object that represents the item. The following example adds a line before the Open Database command on the File menu (added by the preceding example).

    Set databaseItem = CommandBars("Worksheet Menu Bar").Controls("File") _
        .Controls("Open Database")
    databaseItem.BeginGroup = True
    
    

    Adding and Modifying Shortcut Menus

    You can add and modify custom shortcut menus by using the Customize dialog box in Microsoft Access, or by using Visual Basic in Microsoft Excel. In Microsoft Access, Word, and PowerPoint, you can modify built­in shortcut menus (and custom shortcut menus in Microsoft Access) by using the Customize dialog box.

    With Microsoft Access, you can assign custom shortcut menus to reports, forms, and controls on forms; the assigned shortcut menu is displayed whenever the user right­clicks the corresponding object. For information about working with shortcut menus in Microsoft Access reports and forms, see Chapter 1 in Building Applications with Microsoft Access 97.

    Microsoft Excel provides an event — BeforeRightClick — that you can respond to by modifying a built­in shortcut menu or displaying a custom shortcut menu.

    Note   Word and PowerPoint don't provide a way to display a custom shortcut menu when the user right­clicks in the application window.

    Using the Customize Dialog Box

    In Microsoft Access, the Customize dialog box provides a convenient way to add a custom shortcut menu.

    To add a shortcut menu in Microsoft Access

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. On the Toolbars tab, click New.

    3. In the Toolbar name box, type a name for the new shortcut menu, and then click OK.

      An empty, floating toolbar with the name you typed is displayed.

    4. Click Properties to display the Toolbar Properties dialog box.

    5. In the Type box, click Shortcut Menu.

      The empty, floating toolbar is no longer displayed. To display the shortcut menu from the Customize dialog box, click the Toolbars tab, and then select the check box next to Shortcut Menus in the Toolbars box. The toolbar that contains all the shortcut menus is displayed; the shortcut menu you just created appears on the last menu on the toolbar.

    In Microsoft Access, Word, and PowerPoint, you can modify built­in shortcut menus by using the Customize dialog box. (In Microsoft Access, you can also modify custom shortcut menus in this way.) To add a submenu to a shortcut menu, follow the same steps as are given in "Adding Submenus" earlier in this section. To add a command, follow the same steps as are given in "Adding and Grouping Commands" earlier in this section. Remember to select the check box next to Shortcut Menus in the Toolbars box to display a toolbar that contains all the shortcut menus that are available while the Customize dialog box is open.

    Using Visual Basic

    You use the Add method of the CommandBars collection to create a new shortcut menu; setting the Position argument of the Add method to msoBarPopup indicates that the CommandBar object you're creating can be displayed as a shortcut menu. The following example creates a new shortcut menu named "Shortcuts1."

    Set cstm = CommandBars.Add(Name:="Shortcuts1", Position:=msoBarPopup, _
        MenuBar:=False, Temporary:=False)
    
    

    In Microsoft Excel, Word, and PowerPoint, you must use Visual Basic to create a new shortcut menu. In Microsoft Access, you have the option of using either Visual Basic or the Customize dialog box.

    To modify a custom or built­in shortcut menu in any Microsoft Office application by using Visual Basic, you use the same techniques as are described earlier in this section for using Visual Basic to add submenus or commands to a menu. You use CommandBars(name), where name is the name of a shortcut menu, to return a CommandBar object that represents that shortcut menu. Then you can add or modify the elements of the Controls collection available from that CommandBar object.

    Deleting Menu Components

    You can delete built­in or custom items from menus, you can delete built­in or custom menus from menu bars, and you can delete custom menu bars. Note, however, that although you can delete all the items on shortcut menus and built­in menu bars, you cannot delete the shortcut menus or built­in menu bars themselves.

    Deleting built­in menu components can help you tailor your Visual Basic application to the needs of the user. For example, you might want to delete a built­in command from a menu and replace it with a custom version of the command that performs specialized tasks for the user. Or you might want to remove certain menu items to simplify the interface or reduce the possibility that inexperienced users will choose commands you didn't intend for them to use.

    Note   You can restore built­in menu bars, menus, or menu items that you've deleted. However, you cannot restore custom menu bars, menus, or menu items that you've deleted; you must re­create them.

    Using the Customize Dialog Box

    With the Customize dialog box open, you can delete any menu component.

    To delete a menu system component

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. If the menu bar that contains the menu component you want to delete isn't visible, select the check box next to the name of that menu bar in the Toolbars box on the Toolbars tab.

    3. Right­click the menu component you want to delete, and then click Delete on the shortcut menu.

    To delete an entire custom menu bar, open the Customize dialog box, click the name of the menu bar in the Toolbars box on the Toolbars tab, and then click the Delete button. You cannot delete built­in menu bars.

    Using Visual Basic

    Use the Delete method to delete a custom menu bar, a custom or built­in drop­down menu or submenu, or a custom or built­in menu item. You cannot delete a built­in menu bar or a shortcut menu.

    The following Microsoft Excel example deletes the Edit menu from the menu bar for charts.

    CommandBars("Chart Menu Bar").Controls("Edit").Delete
    
    

    The following example deletes the custom menu bar named "Custom Menu Bar."

    CommandBars("Custom Menu Bar").Delete
    
    

    For information about restoring built­in menu components that you've deleted, see the following section.

    Restoring Built­in Menu Components

    You can restore built­in menu bars, menus, or menu items that you've deleted. However, you cannot restore custom menu bars, menus, or menu items that you've deleted; you must re­create them.

    Using the Customize Dialog Box

    You can use the Customize dialog box to restore a built­in menu or submenu to once again contain its original, built­in set of menu items. Note that if you restore a menu, all the submenus on that menu are restored. Likewise, if you restore a built­in menu bar, all the menus and submenus on that menu bar are restored.

    To restore a built­in menu

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. If the menu bar that contains the menu you want to restore isn't visible, select the check box next to the name of that menu bar in the Toolbars box on the Toolbars tab.

    3. Right­click the menu or submenu you want to delete, and then click Restore on the shortcut menu.

    To restore a built­in menu bar, open the Customize dialog box, click the name of the menu bar in the Toolbars box on the Toolbars tab, and then click the Restore button.

    Using Visual Basic

    Use the Reset method to reset the components of a built­in menu bar, menu, or submenu.

    The following Microsoft Excel example resets the Edit menu on the menu bar for charts.

    CommandBars("Chart Menu Bar").Controls("Edit").Reset
    
    

    The following Word example resets the built­in menu bar.

    CommandBars("Menu Bar").Reset
    
    

    Run­Time Modifications to the Menu System

    You can program the menu system you created at design time to respond dynamically to changing conditions at run time. You can replace the default menu bar with a custom menu bar that you've designed. If a particular menu item is an inappropriate choice in certain contexts, you can remove it, hide it, or disable it to prevent the user from selecting it (disabling a menu item is also called dimming the menu item, or making it gray). If a menu item represents an option with two possible states, you can make the command's button image appear pushed down to show that the option is turned on or appear flat to show that it's turned off. Finally, you might want to rename a menu item in response to current conditions. For example, in Microsoft Excel, clicking the Freeze Panes command on the Windows menu causes the command to be renamed Unfreeze Panes.

    Note that although you can make design­time changes to the menu system by using either the Customize dialog box or Visual Basic, you must use Visual Basic to make any run­time changes.

    Displaying a Custom Menu Bar

    To display a custom menu bar instead of the active menu bar, you set the Visible property of a CommandBar object that represents that custom menu bar to True. (For information about creating a menu bar that can replace the active menu bar, see "Adding a Custom Menu Bar" earlier in this chapter.) The newly visible menu bar replaces the active menu bar automatically. You set the Visible property to False to display the default menu bar again when your Visual Basic application finishes running.

    Whenever a user starts a Microsoft Office application, the default menu bar is displayed. In Word, you can replace the default menu bar with a custom menu bar at startup — the last menu bar that was visible when the Normal template was saved before quitting is the default menu bar when Word is started again. You can also set the Visible property of a menu bar to True in an Open event procedure to replace the default menu bar. In Microsoft Excel and PowerPoint, you must use an event procedure or a macro to replace the default menu bar.

    For information about specifying form, report, and global menu bars in Microsoft Access, see Chapter 1 in Building Applications with Microsoft Access 97.

    Displaying Menu Components Dynamically

    If a menu component applies only to a particular document, it's best if that menu component appears only when that document is active; this reduces needless clutter in the interface. You can limit the lifetime of a given menu component to the period during which the document it applies to is open or active.

    If you want a menu or menu item to appear only for a specific document, you can set the Visible property to make the component visible every time the user activates the document, and hide it every time the user deactivates the document. If you want to associate a menu bar with a specific document, you can set the Visible property to make the menu bar appear whenever the user activates the document and then hide the menu bar whenever the user deactivates the document, rather than actually adding or deleting the menu bar each time.

    To display menu components dynamically, you write the appropriate event procedure that enables the component or makes it visible, and you write the event procedure that disables the component or hides it. If the application whose menu bars you're modifying doesn't support events, you cannot customize the interface dynamically. An alternative in these applications is to assign similar procedures to the OnAction property of other menu items or toolbar buttons. If your application supports embedding ActiveX controls, you can also modify the interface of the container application in response to an event supported by that control.

    Note   Because Word stores customizations in documents and templates, custom menu components are visible when the document or template is available in the current context, and they're hidden when the document or template isn't available. In contrast, because Microsoft Excel stores customizations at the workspace level, you need to use the Visible property in event code to dynamically change the interface.

    Enabling or Disabling Menu Components

    If you want to prevent the user from choosing a particular menu item under certain conditions, you can disable it. A disabled command still appears on the menu, but it appears dimmed and doesn't respond to user actions. Use the Enabled property to enable or disable a menu item. The Enabled property is True if the menu item is enabled, and it's False if the menu item is disabled (you cannot set the Enabled property for a built­in menu item). The following Microsoft Excel example adds the Open Database command to the File menu on the menu bar for worksheets and then disables the Open Database command.

    CommandBars("Worksheet Menu Bar").Controls("File") _
        .Controls.Add("Open Database").Enabled = False
    
    

    If you want to disable all the commands on a particular menu, you can disable the menu itself. This effectively disables all the commands on the menu, as the user no longer has access to them. The following Microsoft Excel example disables the entire File menu on the menu bar for worksheets.

    CommandBars("Worksheet Menu Bar").Controls("File").Enabled = False
    
    

    Note   You can disable all the menu items on a submenu, but you cannot disable the submenu itself.

    Indicating the State of a Menu Item

    If a menu item represents an option that has only two possible states, you can make the button image next to the item appear pushed down or appear flat to indicate the current state of the option. The appearance should be changed to the opposite of its current appearance — and the option turned on or off, accordingly — each time the user clicks the menu item. You change the appearance by setting the State property of the menu item.

    To see how this works, suppose that the Microsoft Excel procedure in the following example is assigned to the custom menu item Database on the View menu on the menu bar for worksheets. This menu item offers the user the option of viewing a worksheet either in database view or in worksheet view. Every time the user clicks the Database menu item, the procedure switches the button image next to the menu item between appearing pushed down and appearing flat (that is, the procedure sets the State property and then switches views).

    Sub DatabaseView()
    	With CommandBars("Worksheet Menu Bar").Controls("View").Controls("Database")
    	If .State = msoButtonUp Then
    		.State = msoButtonDown
    		'Switch to database view
    	Else
    		.State = mosButtonUp
    		'Switch to worksheet view
    	End If
    	End With
    End Sub
    
    

    Every built­in and custom menu item has text and a button image; many built­in menu items have blank button images. When you add an item to a menu by using the Customize dialog box, you can specify and modify the item's button image. At run time, you set the menu item's FaceId property to specify the button image you want to display next to the menu item when its state changes. To specify the button image you want to display next to a menu item, you must find the built­in command with that button image, determine its ID, and then assign that value to the FaceId property. (Changing the FaceId property of a menu item doesn't change its functionality.) For information about determining the built­in command ID numbers of a Microsoft Office application, see "Menu Item and Toolbar Control IDs" later in this chapter.

    The following Microsoft Excel example not only switches the state of the button image next to the menu item, but it changes the image as well. When the user switches to database view, the button image is switched to a grid (ID 987). Likewise, when the user switches out of database view, the image is set to a blank face (ID 1).

    Sub DatabaseView()
    	With CommandBars("Worksheet Menu Bar").Controls("View").Controls("Database")
    	If .State = msoButtonUp Then
    		.FaceId = 987
    		.State = msoButtonDown
    		'Switch to database view
    	Else
    		.FaceId = 1
    		.State = mosButtonUp
    		'Switch to worksheet view
    	End If
    	End With
    End Sub
    
    

    Renaming a Menu Item

    You can use the Caption property of a menu item to change the item's name in response to changing conditions in your Visual Basic code. Suppose, for example, that you've created a menu command that opens a database. After the user has opened a database, you may want to replace the original command with a command that closes the database. The following example shows how you can accomplish this.

    CommandBars("MyMenubar").Controls("File").Controls("Open Database") _
    	.Caption = "Close &Database"
    
    

    When you rename a menu item this way, make sure that the other procedures in your application reference the menu item by its new name (Close Database, in this example).

    You can also use variables to refer to a menu item. An advantage of this technique is that variables continue to work even if the item's caption changes. The following example sets a variable to the Open Database menu item.

    Set openData = CommandBars("My Menubar").Controls _
    	("File").Controls("Open Database")
    
    

    You can change the caption later by using the code in the following example.

    openData.Caption = "Close &Database"
    
    

    Toolbars

    Each Microsoft Office application provides a system of toolbars containing toolbar controls that the user can click to gain access to frequently used commands. Each toolbar can appear docked at the top, at the bottom, or on either the left or right side of the application window, or as a floating window positioned anywhere in the workspace. Each toolbar control is a simple, graphical control with which the user can exchange information with your Visual Basic application. To display any toolbar in an Office application, point to Toolbars on the View menu, and then click the name of the toolbar you want to display. To see additional available toolbars, open the Customize dialog box and browse through the toolbars listed in the Categories box.

    There are several types of controls that are classified as toolbar controls; these are discussed in the following paragraphs.

    The most common type of toolbar button is a simple button control that contains a graphic. The graphic, called the button image, is a visual representation of the command or option that the toolbar button activates. The user can click one of these toolbar buttons to execute a command (for example, clicking the New button on the Standard toolbar creates a new document) or to alternate between the two possible states of an option represented by a button (for example, clicking the Bold button on the Formatting toolbar alternately applies bold formatting to and removes it from the selected text).

    Another type of toolbar control is a button control that contains a graphic and an attached drop­down palette. The user clicks the drop­down arrow to display a palette and then clicks an option on the palette. The user clicks the button control to apply the current option. For example, in Microsoft Excel, clicking the drop­down arrow for the Font Color button displays a palette of font colors from which the user can choose. Clicking this toolbar control's button applies the indicated color to the selected text.

    A text box, list box, or combo box can also be a toolbar control. The user either types text in the box or clicks the drop­down arrow and then clicks an item in the list. For example, on the Formatting toolbar, you can set the font size for the selected text either by clicking an item in the drop­down list box contained in the Font Size button or by typing an entry in the text box.

    The last type of toolbar control is the pop­up control, which displays a menu of other controls. A pop­up control on a toolbar is essentially the same as a menu name on a menu bar. The Draw button on the Drawing toolbar in Microsoft Excel, Word, or PowerPoint is an example of a pop­up control.

    Note   Although they share similar appearances and behavior, toolbar controls and ActiveX controls aren't the same. You cannot add ActiveX controls to toolbars, and you cannot add toolbar controls to documents or forms.

    Now that you understand what toolbars and the various types of toolbar controls are, you can study the specifics of modifying the toolbars and toolbar controls described in the preceding paragraphs. In the following sections, you'll learn how to make design­time and run­time changes to toolbars and toolbar controls.

    Guidelines for Customizing Toolbars

    The Microsoft Office applications offer you a wide range of ways to modify the built­in toolbars to better serve the needs of the user. You can create new toolbars; add new toolbar buttons to built­in or custom toolbars; modify the image on a toolbar button face; and assign macros, ToolTip text, and status bar text to toolbar buttons.

    Whether you modify a built­in toolbar or create a new one depends on the extent of the changes you want to make. Modifying a built­in toolbar makes sense if you're adding or changing only a few toolbar buttons; creating a new toolbar may be more convenient if you want to provide an entirely different assortment of commands than are found on any of the built­in toolbars, or if you want to present a number of custom toolbar buttons as a distinct group. Regardless of how many changes you make, you can restore the built­in menu system to its default state whenever you want.

    In addition to the above changes, which are usually made at design time, you can use Visual Basic procedures to change the properties of toolbars and toolbar buttons in response to user input while your application is running (that is, at run time). For example, you can hide a toolbar when the user no longer needs it, move or resize a toolbar to keep it out of the user's way, disable a toolbar button to prevent the user from clicking it at an inappropriate time, or switch the appearance of a toolbar button between pushed down and flat every time the user clicks it.

    Using Menus

    You can add pop­up controls — the same controls that display menus on menu bars and submenus on menus — to any built­in or custom toolbar. Often, adding a menu to a toolbar is a useful compromise between customizing a built­in menu bar (which may not be as convenient as adding toolbar controls) and adding a cumbersome number of toolbar controls (some of which may be dropped from a wide toolbar that's docked). The Draw button on the Drawing toolbar in Microsoft Excel, Word, or PowerPoint is an example of a menu on a toolbar.

    To add menus, submenus, and menu items to toolbars, use the same steps as were presented for adding such components to menu bars in "Design­Time Modifications to the Menu System" earlier in this chapter.

    Using Text Boxes, List Boxes, and Combo Boxes

    In the Microsoft Office applications, you can add text boxes, list boxes, and combo boxes to built­in and custom toolbars. These controls can be useful for getting information from a user frequently, or for running a complex procedure that can use the value of the control to determine a range of possible results.

    The Customize dialog box supports adding built­in text boxes, list boxes, and combo boxes to any toolbar, but it doesn't support adding custom ones; instead, you must use Visual Basic to add and design these controls. With the Customize dialog box open, you can change the width of any built­in or custom text box, list box, or combo box.

    Design­Time Modifications to Toolbars

    Design­time changes include creating a new toolbar; adding new or built­in toolbar controls to a toolbar; deleting toolbar controls from a toolbar; grouping or ungrouping toolbar controls; and changing the width of text box, list box, and combo box toolbar controls. You can also select a new image or use the Button Editor to customize the image associated with a particular toolbar control.

    Adding a Custom Toolbar

    In many cases, you can present a new set of commands by adding custom toolbar controls to a built­in toolbar. But if you want to present a complete set of commands in an easily accessible form, distinct from all built­in commands, you can create a new toolbar. You do this by using either the Customize dialog box or Visual Basic.

    Using the Customize Dialog Box

    The Customize dialog box provides a convenient way to add a custom toolbar.

    To add a toolbar

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. On the Toolbars tab, click New.

    3. In the Toolbar name box, type a name for the new toolbar, and then click OK.

      An empty, floating toolbar with the name you typed is displayed.

    The new toolbar is added to the list in the Toolbars box on the Toolbars tab.

    Using Visual Basic

    You use the Add method of the CommandBars collection to create a new toolbar; setting the Position argument of the Add method to msoBarLeft, msoBarTop, msoBarRight, msoBarBottom, or msoBarFloating indicates whether the CommandBar object you're creating is a floating toolbar or a docked toolbar. The following example creates and displays a new toolbar named "Custom Tools."

    Set cstm = CommandBars.Add(Name:="Custom Tools", Position:=msoBarFloating, _
        MenuBar:=False, Temporary:=False)
    cstm.Visible = True
    
    

    Adding and Grouping Controls

    You can add controls to any built­in or custom toolbar, and you can visually separate them (with lines) into logical groupings.

    Using the Customize Dialog Box

    The Customize dialog box provides an easy method for adding controls to toolbars.

    To add a built­in control to a toolbar

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. If the toolbar you want to modify isn't visible, select the check box next to the name of that toolbar in the Toolbars box on the Toolbars tab.

    3. On the Commands tab, click a category of commands in the Categories box.

      The commands in the category you select are now listed in the Commands box.

    4. Drag a control from the Commands box to the position on the menu where you want to add the control.

      A vertical I­beam on the toolbar indicates the position where the control will be added when you release the mouse button.

    Tip   You can easily make a copy of any built­in toolbar control by displaying the toolbar that contains that control and holding down CTRL while you drag the control to another toolbar.

    The Customize dialog box also provides a quick way to add a custom command to a built­in or custom toolbar. However, each Microsoft Office application has a different technique for using the Customize dialog box to do this. The following paragraphs describe these differences.

    Microsoft Access   To add a control that runs a macro, follow the same steps as in the procedure for adding a built­in control to a toolbar. In the Categories box, click All Macros. Drag the macro you want from the Commands box to the position on the toolbar where you want it to appear. To add a control that runs a Function procedure, follow the same steps as in the procedure for adding a built­in control to a toolbar. In the Categories box, click any category and drag any item you want to the position on the toolbar where you want it to appear. Right­click the control, and then click Control Properties to open the Control Properties dialog box. In the Caption box, delete the current name, and then type the new name for your control. In the On Action box, type an expression to run your Visual Basic Function procedure. The expression must use the following syntax: =functionname().

    Microsoft Excel   Follow the same steps as in the procedure for adding a built­in control to a toolbar; in the Categories box, click Macros, and then drag the Custom Button control from the Commands box to the position on the toolbar where you want the control to appear. Right­click the new control, and then click Assign Macro. In the Assign Macro dialog box, select the macro you want to run. Use the commands on the shortcut menu to change the image of the control.

    Microsoft Word   Follow the same steps as in the procedure for adding a built­in control to a toolbar; in the Categories box, click Macros, and then drag a macro from the Commands box to the position on the toolbar where you want the control to appear. Use the commands on the shortcut menu for the new control to change the control's name, image, and other display properties.

    Tip   In Word, if you write a procedure whose name is the same as that of a built­in Word control (or if you write a procedure named "MAIN" in a module whose name is the same as that of a built­in Word command), that procedure will replace the built­in functionality of the control whenever the module that contains it is available in the current context. Every copy of the control on whatever toolbar it appears will run the replacement procedure. For more information about controlling the context of your customizations, see "Scope of Changes to the User Interface" earlier in this chapter. For more information about modifying Word commands, see Chapter 7, "Microsoft Word Objects."

    Microsoft PowerPoint   Follow the same steps as in the procedure for adding a built­in control to a toolbar; in the Categories box, click Macros, and then drag a macro from the Commands box to the position on the toolbar where you want it to appear. Use the commands on the shortcut menu for the new control to change the control's name, image, and other display properties.

    Modifying the Appearance of a Toolbar Button

    The face of a button on a toolbar can be either the button image alone, the button name alone, or the button image displayed next to the name. Whether a button appears with just an image, just a name, or both is determined by its "style." You set a button's style using commands on the shortcut menu while the Customize dialog box is open. The following table describes the effect of each style on toolbar buttons.

    StyleWhat appears on a toolbar button
    Default StyleButton image only
    Text Only (In Menus) Button image only
    Text Only (Always) Name only
    Image And TextButton image and name

    While the Customize dialog box is open, you can add or modify the image on a toolbar button. The following table describes the techniques you can use.

    ToDo this
    Use a predefined image Right­click the button, point to Change Button Image, and then click the image you want.
    Copy and paste another button's image Right­click the button that has the image you want to use, and then click Copy Button Image. Right­click the button whose image you're customizing, and then click Paste Button Image.
    Copy and paste an image from a graphics program Open the image you want to copy in a graphics program. Select and copy the image (preferably a 16 x 16 pixel image or portion). Switch back to your application. Right­click the button, and then click Paste Button Image.
    Edit the button's current image Right­click the button, and then click Edit Button Image. In the Button Editor dialog box, you can change the color and shape of the image, adjust the image's position on the button, and preview your changes to the image. When you finish editing the image, click OK.
    Reset a button to use its original image Right­click the button, and then click Reset Button Image.

    Tip   In Microsoft Access, you can use the Properties dialog box to set many other properties of menu commands. For more information, see Chapter 1 in Building Applications with Microsoft Access 97.

    Grouping Controls

    You can separate groups of related controls on a toolbar, using lines. The lines themselves aren't controls; rather, you can set each control on a toolbar to appear with a line before it. Use the Customize dialog box to set a command to appear as the first control in a group of controls.

    To begin a group of buttons on a toolbar

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. If the toolbar that contains the control you want to modify isn't visible, select the check box next to the name of that toolbar in the Toolbar box on the Toolbars tab.

    3. Right­click the control you want to appear with a line before it, and then click Begin Group.

      The next time you right­click that control, a check mark will be displayed next to Begin Group on the shortcut menu. To remove the line before a control, right­click the control and then click Begin Group again (the check mark will no longer appear).

    Using Visual Basic

    Use the Add method of the CommandBarControls collection to add a new control to the CommandBar object that represents a particular toolbar. To add a built­in control, you specify the ID number of the control by using the Id argument of the Add method. The following example adds the Spelling control to the toolbar named "Quick Tools."

    Set mySpell = CommandBars("Quick Tools").Controls.Add(Id:=2)
    
    

    For information about determining the built­in command ID numbers of an Office application, see "Menu Item and Toolbar Control IDs" later in this chapter.

    To add a custom control, you add a new control and then set the OnAction property to specify a Visual Basic procedure to run whenever that control is clicked. Setting the Type argument of the Add method to msoControlButton indicates that a control is a button. Set the FaceId value of the control to the ID of a built­in control whose face you want to copy. The following Microsoft Excel example adds a button before the Save button on the Standard toolbar. Microsoft Excel runs the OpenDatabaseProc Visual Basic procedure whenever the user clicks the menu item. The example also sets the image on the button to a grid (ID 987).

    Set databaseItem = CommandBars("Standard").Controls. _
        Add(Type:=msoControlButton, Before:=3)
    With databaseItem
        .OnAction:="OpenDatabaseProc"
        .FaceId = 987
    End With
    
    

    There are many properties of the objects that represent toolbar buttons that you can set in Visual Basic to modify the appearance of a control. For more information, see "Style Property" and "FaceID Property" in Help, as well as the Help topics for other properties and methods of the CommandBarButton object.

    To set a control to begin a group of controls (that is, to be preceded by a line), just set the BeginGroup property of the CommandBarButton, CommandBarPopup, or CommandBarComboBox object that represents that control to True. To remove the line, set the BeginGroup property of the appropriate object to False. Use Controls(index), where index is the caption or index number of a control, to return an object that represents the control.

    Adding and Initializing Text Box, List Box, and Combo Box Controls

    You can add built­in text box, list box, and combo box controls by using the Customize dialog box. Use the same steps that were given earlier in this section for adding built­in controls.

    To add and initialize the contents of custom text box, list box, and combo box controls, you must use Visual Basic. You use the Add method of the CommandBarControls collection to add a text box, list box, or combo box; the Type argument indicates the kind of control you're adding, as shown in the following table.

    To add this control Specify this type
    Text boxmsoControlEdit
    List boxmsoControlDropDown
    Combo boxmsoControlComboBox

    You can use the Style property of the text box, list box, or combo box to indicate whether the caption of the control should appear to the left of the box itself.

    The following example adds a combo box with the label "Quarter" to a custom toolbar and assigns the macro named "ScrollToQuarter" to the combo box.

    Set newCombo = CommandBars("Custom1").Controls _
        .Add(Type:=msoControlComboBox)
    With newCombo
        .AddItem "Q1"
        .AddItem "Q2"
        .AddItem "Q3"
        .AddItem "Q4"
        .Style = msoComboNormal
        .OnAction = "ScrollToQuarter"
    End With
    
    

    While your Visual Basic application is running, the procedure assigned to the OnAction property of the combo box control is called each time the user changes the control. In the procedure, you can use the ActionControl property of the CommandBars object to find out which control was changed and to return the changed value. The ListIndex property will return the item that was entered in the combo box.

    Deleting Toolbar Controls

    Deleting built­in toolbar controls can help you tailor your Visual Basic application to the needs of the user. For example, you may want to delete a built­in control from a toolbar and replace it with a custom version of that command, which will perform specialized tasks for the user. Or you may want to remove certain controls to simplify the interface or reduce the possibility that inexperienced users will choose commands you didn't intend for them to use.

    Note   You can restore built­in toolbars or toolbar controls that you've deleted. However, you cannot restore custom toolbars or toolbar controls that you've deleted; you must re­create them.

    Using the Customize Dialog Box

    With the Customize dialog box open, you can delete any toolbar control.

    To delete a toolbar control

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. If the toolbar that contains the toolbar control you want to delete isn't visible, select the check box next to the name of that toolbar in the Toolbars box on the Toolbars tab.

    3. Right­click the control you want to delete, and then click Delete on the shortcut menu.

    To delete an entire custom toolbar, open the Customize dialog box, click the name of that toolbar in the Toolbars box on the Toolbars tab, and then click the Delete button. You cannot delete built­in toolbars.

    Using Visual Basic

    Use the Delete method to delete a custom toolbar or a custom or built­in toolbar control. You cannot delete a built­in toolbar.

    The following Microsoft Excel example deletes the Print control from the Standard toolbar.

    CommandBars("Standard").Controls("Print").Delete
    
    

    The following example deletes the custom toolbar named "Custom Bar."

    CommandBars("Custom Bar").Delete
    
    

    You can restore built­in toolbar controls that you've deleted. For more information, see the following section.

    Restoring Built­in Toolbar Controls

    You can restore built­in toolbar controls that you've deleted. However, you cannot restore custom toolbars or toolbar controls that you've deleted; you must re­create them.

    Using the Customize Dialog Box

    You can use the Customize dialog box to restore a built­in toolbar to its built­in set of controls.

    To restore a built­in toolbar

    1. If the Customize dialog box isn't already open, point to Toolbars on the View menu, and then click Customize.

    2. On the Toolbars tab, select the built­in toolbar you want to restore.

    3. Click Restore.

    Using Visual Basic

    Use the Reset method to reset the components of a built­in toolbar.

    The following Microsoft Excel example resets the Standard toolbar to its default set of controls.

    CommandBars("Standard").Reset
    
    

    Run­Time Modifications to Toolbars

    You can program the toolbars you create at design time to respond dynamically to changing conditions at run time. If a particular control is an inappropriate choice in certain contexts, you can remove it or disable it to prevent the user from clicking it. If a control represents an option with two possible states, you can make the control appear pushed down to show that the option is turned on or appear flat to show that it's turned off.

    Note that although you can make design­time changes to toolbars by using either the Customize dialog box or Visual Basic, you must use Visual Basic to make any run­time changes.

    Displaying or Hiding Toolbars and Toolbar Controls

    A toolbar takes up screen space that could otherwise be used to display data; you can display a toolbar when necessary and hide it when the user no longer needs it. A toolbar is visible if its Visible property is True, and it's not visible if this property is False. Setting this property to True corresponds to selecting the check box next to the name of that toolbar on the Toolbars tab in the Customize dialog box and then clicking OK.

    The following Microsoft Excel procedure, which is assigned to the View MyToolbar menu item on the View menu, switches the state of the menu item and the Visible property of the toolbar every time the user clicks the menu item. When the toolbar is made visible, it reappears in the same position it occupied when it was made invisible.

    Sub ViewMyAppToolbar()
    	With CommandBars("Worksheet Menu Bar").Controls("View").Controls("View MyToolbar")
    		If .State = msoButtonUp Then
    			.State = msoButtonDown
    			CommandBars("MyAppTools").Visible = True
    		Else
    			.State = msoButtonUp
    			CommandBars("MyAppTools").Visible = False
    		End If
    	End With
    End Sub
    
    

    When a toolbar is visible, the user can click any control on it to run that control's assigned procedure.

    If you want a specific toolbar control to appear only when certain conditions exist, you can hide or show the toolbar control at run time. By setting the Visible property to True or False, you can effectively add a control to or remove a control from the user's workspace without actually deleting the control.

    Note   Because Word stores customizations in documents and templates, custom toolbars and toolbar controls are visible when the document or template is available in the current context, and they're hidden when the document or template isn't available. In contrast, because Microsoft Excel stores customizations at the workspace level, you need to use the Visible property in event code to dynamically change the interface.

    Moving and Resizing Toolbars

    You may want to adjust the prominence of a toolbar on the screen in response to changing conditions while your application is running. You can do this by changing the size or position of the toolbar. Toolbars support several properties you can use to resize them; to dock them at the top, bottom, left edge, or right edge of the application window; or to position them elsewhere on the screen (if they're are floating toolbars). For more information about the properties and methods you can use with CommandBar objects that represent toolbars, see "CommandBar Object" in Help, and use the jumps at the top of the topic to display the lists of properties and methods.

    Restoring a Built­in Toolbar

    If one of the default toolbars has been modified — either by a user or by a Visual Basic procedure — you can return the toolbar to its default state by using the Reset method. Using this method corresponds to selecting the name of the customized built­in toolbar on the Toolbars tab in the Customize dialog box and then clicking Reset.

    The following example resets all the toolbars to their default state and simultaneously deletes all the custom toolbars.

    For Each cb In CommandBars
    	If cb.BuiltIn Then
    		cb.Reset
    	Else
    		cb.Delete
    	End If
    Next
    
    


      Caution   Be careful when you use the Reset method; it not only restores any built­in toolbar controls that have been deleted, but it also deletes any custom toolbar controls that have been added. Keep in mind that another macro may have added custom toolbar controls to the toolbar, and resetting the toolbar will remove these controls as well. To avoid these problems, remove any toolbar controls added by your application one by one, without resetting the entire toolbar.


    Enabling or Disabling Toolbar Controls

    You may want to control the availability of a toolbar control while your application is running, to prevent the user from clicking the button at inappropriate times. To do this, you can dynamically enable and disable the toolbar control. When a toolbar control is disabled, it beeps when it's clicked and doesn't run the procedure associated with it. Use the Enabled property to set or return the state (enabled or disabled) of a toolbar control.

    The following example disables button three on the Standard toolbar.

    CommandBars("Standard").Controls(3).Enabled = False
    
    

    Indicating the State of Toolbar Buttons

    If a toolbar button represents an option with two possible states, you can change the appearance of the button to indicate the current state of the option: When the option is turned on, the associated button appears pushed down; when the option is turned off, the button appears flat.

    The State property for a toolbar button is msoButtonDown if the button appears pushed down; this property is msoButtonUp if the button appears flat. The following procedure, which is assigned to the new toolbar control Database View, changes the appearance of the control before switching between special views on the worksheet.

    Sub DatabaseView()
    	With CommandBars("MyAppToolbar").Controls(3)
    		If .State = msoButtonDown Then
    			.State = msoButtonUp
    			'Switch to database view
    		Else
    			.State = msoButtonDown
    			'Switch to worksheet view
    		End If
    	End With
    End Sub
    
    

    Modifying Text Box, List Box, and Combo Box Controls

    If you add custom text box, list box, or combo box controls to a toolbar, you can make run­time changes such as changing the current value of the text box portion of the control and adding or removing items from the list portion of the control (for list boxes and combo boxes only).

    You can set the Text property of a text box, list box, or combo box control to reflect a change in the state of your Visual Basic application. For example, if the user clicks a toolbar button that runs a procedure named "MaxZoom" (a custom procedure that displays the active document at maximum zoom), the text box portion of a combo box control that's used to adjust the zoom more precisely and display a percentage value can be set to the maximum zoom percentage.

    You can use the AddItem and RemoveItem methods to add and remove items (by index number) from the list portion of a list box or combo box control. For example, if you created a list box control in Word that tracks the styles the user applies during a session, you can add the name of a style to the list portion of the control each time the user applies a style.

    Note   Be careful when you add or remove an item in a list box or combo box control; this causes the index numbers of all the items to shift.

    You can use other properties and methods of list box and combo box controls to change the appearance of a control at run time. For example, you can add and adjust a header list for the control (a header list is the group of list items at the top of the list portion of a control that are separated from the rest of the list items by a line.) For more information about using text box, list box, and combo box controls, see "Using command bars" in Help.

    Menu Item and Toolbar Control IDs

    Each Office application contains a unique set of menu bars and toolbars and a unique set of available menu items and toolbar controls. (Note that only a subset of the available menu items and toolbar controls actually appears on an application's built­in menu bars and toolbars.) Each application stores its menu bars and toolbars in a unique way. For information about how menu bars and toolbars are stored, see "Scope of Changes to the User Interface" earlier in this chapter.

    Whereas the functionality associated with each built­in menu item and toolbar control belongs to a specific Office application, the caption, button image, width, and other default properties of each menu item and toolbar control are stored in one resource shared by all the applications. You can use ID numbers to find specific menu items and toolbar controls in this resource.

    Note   This resource also contains the default properties of the pop­up controls that display built­in menus. However, those pop­up controls don't contain the built­in menu items on those menus; that is, the pop­up controls are empty.

    Although you can usually ignore the ID of a menu item or control and instead use the Customize dialog box to make changes to a built­in or custom menu or toolbar, you may need to refer to the ID of an item to make certain kinds of changes to your custom interface. The following are some of the situations in which you'll need to refer to an item's ID:

    • You want to assign an item to a built­in or custom menu or toolbar when that item isn't available anywhere in the Customize dialog box at design time.

    • You want to add a built­in item to a menu or toolbar at run time.

    • You want to copy a particular button's image to another button at run time.

    You can assign the ID of a built­in item to the Id argument of the Add method for the CommandBarControls collection, and you can assign the ID of a an item to the FaceId property of any custom or built­in control.

    Note   Even though the shared resource contains information about every menu item and toolbar control in all the Office applications, you can only add items and controls whose functionality is contained in the application you're working in. For example, you cannot add the Microsoft Excel Delete Rows toolbar button (ID 293) to a toolbar in Word. You can, however, copy the face of the Delete Rows toolbar button from Microsoft Excel to a toolbar control in Word.

    To determine the IDs of the built­in menu items and toolbar controls in a specific Office application, you can do any of the following:

    • In a module, write code to assign a menu item or toolbar control that already appears on a menu or toolbar to an object variable, and then use debugging tools to inspect the value of the Id property of that object. Using that ID, you can add a copy of the item or control to another menu or toolbar by using the Add method, or you can copy the image to another button by assigning the ID to another button's FaceId property.

    • Run the following procedure in one of the Office applications to create a text document that lists the IDs and captions of all the built­in commands in that application.
      Sub outputIDs()
      Const maxId = 4000
      Open "c:\ids.txt" For Output As #1
      ' Create a temporary command bar with every
      ' available item and control assigned to it.
      Set cbr = CommandBars.Add("Temporary", msoBarTop, False, True)
      For i = 1 To maxId
          On Error Resume Next
          cbr.Controls.Add Id:=i
      Next
      On Error GoTo 0
      ' Write the ID and caption of each control to the output file.
      For Each btn In cbr.Controls
          Write #1, btn.Id, btn.Caption
      Next
      ' Delete the command bar and close the output file.
      cbr.Delete
      Close #1
      End Sub
      
      

    • Run the following procedure in one of the Office applications to create a set of custom toolbars that contain as many buttons as there are valid FaceId property values in Office; each button's image and ToolTip text is set to one of those values. You can cross­reference the ID of a built­in command (see the preceding procedure) to the FaceId property value of a button on one of these toolbars, and vice versa.
      Sub MakeAllFaceIds()
      'Make fourteen toolbars with 300 faces each.
      'Note that maxId is greater than last valid ID, so
      'error will occur when first invalid ID is used.
      Const maxId = 3900
      On Error GoTo realMax
      For bars = 0 To 13
          firstId = bars * 300
          lastId = firstId + 299
          Set tb = CommandBars.Add
          For i = firstId To lastId
             Set btn = tb.Controls.Add
             btn.FaceId = i
             btn.TooltipText = "FaceId = " & i
          Next
          tb.Name = ("Faces " & CStr(firstId) & " to " _
             & CStr(lastId))
          tb.Width = 591
          tb.Visible = True
      Next
      'Delete the button that caused the error and set toolbar name 
      realMax:
      btn.Delete
      tb.Name = ("Faces " & CStr(firstId) & " to " _
          & CStr(i - 1))
      tb.Width = 591
      tb.Visible = True
      End Sub
      
      

    Note   The IDs of the pop­up controls for built­in menus are in the range 30002 to 30426. Remember that these IDs return empty copies of the built­in menus.