C H A P T E R 8 | Microsoft Office 97/Visual Basic Programmer's Guide |
Menus and Toolbars |
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 CDROM 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.
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 Office applications (excluding Outlook) provide a common interface the Customize dialog box for making designtime changes to your Visual Basic application. Designtime 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 menucomponent and toolbarcontrol 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 designtime 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
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 builtin 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 designtime 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 builtin Word command.
After you've opened the Customize dialog box in any Microsoft Office application, you follow the same general procedure for modifying any builtin or custom menu or toolbar, as described by the following steps.
Note One or more of these options may not be available for builtin 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 "DesignTime Modifications to the Menu System" and "DesignTime Modifications to Toolbars" later in this chapter.
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 designtime 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 toplevel 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 designtime 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 runtime changes.
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.
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:
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:
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 builtin Form View toolbar when your form is opened and hides your custom toolbar when a user closes the form or switches to another form.
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.
Workbooklevel menu bars and toolbars make it easier for you to create a polished user interface for a custom application (an addin, 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
The Attach Toolbars dialog box is displayed.
The name of the menu bar or toolbar you copied appears in the Toolbars in workbook box.
You can delete the original workspacelevel 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 onscreen 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
The Attach Toolbars dialog box is displayed.
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 workspacelevel 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.
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 Addins 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 builtin 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 "DesignTime Modifications to Toolbars" later in this chapter.
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.
Menus are lists of userinterface 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 userinterface enhancements, see Chapter 12, "ActiveX Controls and Dialog Boxes."
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 rightclicks 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 builtin or custom menu bars, add new menu items (commands or submenus) to builtin or custom menus or submenus, add and modify shortcut menus, and assign macros to menu items. In addition, you can restore the builtin menu system to its default state at any time.
Adding Custom Components or Modifying Builtin Components
Each Microsoft Office application comes with its own builtin menu system. You can modify components of this builtin system or create and modify custom menu components.
Modifying a builtin 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 builtin 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 builtin 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 builtin 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 builtin text boxes, list boxes, and combo boxes to menus, use the same techniques given for adding builtin 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 "DesignTime Modifications to Toolbars" later in this chapter).
Designtime 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.
If you want to design a set of menus that differs significantly from what's currently available on the Office application's builtin 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
An empty, floating menu bar with the name you typed is displayed.
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.
You can add a menu to any builtin or custom menu bar. Because a Microsoft Office application can display different builtin 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 builtin 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 builtin or custom menu bar.
To add a custom menu to a menu bar
An Ibeam on the menu bar indicates the position where the new menu will be added when you release the mouse button.
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 builtin menu to a builtin or custom menu bar. You can customize the commands on the copy without affecting the original builtin menu.
To add a copy of a builtin menu to a menu bar
An Ibeam 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 builtin 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 popup 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).
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
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 Ibeam on the menu indicates the position where the submenu will be added when you release the mouse button.
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 popup 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).
You can add commands to any builtin 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 "DesignTime 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 builtin command to a menu or submenu
The commands in the category you select are listed in the Commands box.
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 Ibeam 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 builtin 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 builtin 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 builtin 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 builtin 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. Rightclick 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 builtin 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. Rightclick 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 builtin 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 builtin Word command (or if you write a procedure named "MAIN" in a module whose name is the same as that of a builtin Word command), that procedure will replace the builtin 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 builtin 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.
Style | What appears on a menu |
Default Style | Button image and name |
Text Only (In Menus) | Name only |
Text Only (Always) | Name only |
Image And Text | Button image and name |
Note By default, some builtin 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 builtin 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.
To | Do this |
Use a predefined button image | Rightclick the command, point to Change Button Image, and then click the image you want. |
Copy and paste another command's button image | Rightclick the command that has the image you want to use, and then click Copy Button Image. Rightclick 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. Rightclick the command, and then click Paste Button Image. |
Edit the command's current button image | Rightclick 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) | Rightclick 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
The next time you rightclick that item, a check mark will be displayed next to Begin Group on the shortcut menu. To remove the line before a menu item, rightclick 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 builtin 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 builtin 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.
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.
Set databaseItem = CommandBars("Worksheet Menu Bar").Controls("File") _
.Controls.Add(Type:=msoControlButton, Before:=3)
With databaseItem
.Caption:="Open Database"
.OnAction:="OpenDatabaseProc"
End With
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 builtin 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 rightclicks 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 builtin 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 rightclicks 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
An empty, floating toolbar with the name you typed is displayed.
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 builtin 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 builtin 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.
You can delete builtin or custom items from menus, you can delete builtin 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 builtin menu bars, you cannot delete the shortcut menus or builtin menu bars themselves.
Deleting builtin menu components can help you tailor your Visual Basic application to the needs of the user. For example, you might want to delete a builtin 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 builtin 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 recreate them.
Using the Customize Dialog Box
With the Customize dialog box open, you can delete any menu component.
To delete a menu system component
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 builtin menu bars.
Using Visual Basic
Use the Delete method to delete a custom menu bar, a custom or builtin dropdown menu or submenu, or a custom or builtin menu item. You cannot delete a builtin 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 builtin menu components that you've deleted, see the following section.
Restoring Builtin Menu Components
You can restore builtin 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 recreate them.
Using the Customize Dialog Box
You can use the Customize dialog box to restore a builtin menu or submenu to once again contain its original, builtin set of menu items. Note that if you restore a menu, all the submenus on that menu are restored. Likewise, if you restore a builtin menu bar, all the menus and submenus on that menu bar are restored.
To restore a builtin menu
To restore a builtin 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 builtin 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 builtin menu bar.
CommandBars("Menu Bar").Reset
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 designtime changes to the menu system by using either the Customize dialog box or Visual Basic, you must use Visual Basic to make any runtime changes.
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 builtin 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 builtin and custom menu item has text and a button image; many builtin 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 builtin 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 builtin 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
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"
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 dropdown palette. The user clicks the dropdown 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 dropdown 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 dropdown 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 dropdown 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 popup control, which displays a menu of other controls. A popup 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 popup control.
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 designtime and runtime changes to toolbars and toolbar controls.
Guidelines for Customizing Toolbars
The Microsoft Office applications offer you a wide range of ways to modify the builtin toolbars to better serve the needs of the user. You can create new toolbars; add new toolbar buttons to builtin 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 builtin toolbar or create a new one depends on the extent of the changes you want to make. Modifying a builtin 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 builtin 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 builtin 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 popup controls the same controls that display menus on menu bars and submenus on menus to any builtin or custom toolbar. Often, adding a menu to a toolbar is a useful compromise between customizing a builtin 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 "DesignTime 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 builtin 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 builtin 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 builtin or custom text box, list box, or combo box.
Designtime changes include creating a new toolbar; adding new or builtin 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.
In many cases, you can present a new set of commands by adding custom toolbar controls to a builtin toolbar. But if you want to present a complete set of commands in an easily accessible form, distinct from all builtin 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
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
You can add controls to any builtin 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 builtin control to a toolbar
The commands in the category you select are now listed in the Commands box.
A vertical Ibeam 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 builtin 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 builtin 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 builtin 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 builtin 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. Rightclick 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 builtin 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. Rightclick 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 builtin 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 builtin Word control (or if you write a procedure named "MAIN" in a module whose name is the same as that of a builtin Word command), that procedure will replace the builtin 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 builtin 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.
Style | What appears on a toolbar button |
Default Style | Button image only |
Text Only (In Menus) | Button image only |
Text Only (Always) | Name only |
Image And Text | Button 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.
To | Do this |
Use a predefined image | Rightclick the button, point to Change Button Image, and then click the image you want. |
Copy and paste another button's image | Rightclick the button that has the image you want to use, and then click Copy Button Image. Rightclick 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. Rightclick the button, and then click Paste Button Image. |
Edit the button's current image | Rightclick 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 | Rightclick 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
The next time you rightclick that control, a check mark will be displayed next to Begin Group on the shortcut menu. To remove the line before a control, rightclick 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 builtin 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 builtin 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 builtin 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 builtin 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 builtin 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 box | msoControlEdit |
List box | msoControlDropDown |
Combo box | msoControlComboBox |
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
Deleting builtin toolbar controls can help you tailor your Visual Basic application to the needs of the user. For example, you may want to delete a builtin 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 builtin toolbars or toolbar controls that you've deleted. However, you cannot restore custom toolbars or toolbar controls that you've deleted; you must recreate them.
Using the Customize Dialog Box
With the Customize dialog box open, you can delete any toolbar control.
To delete a toolbar control
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 builtin toolbars.
Using Visual Basic
Use the Delete method to delete a custom toolbar or a custom or builtin toolbar control. You cannot delete a builtin 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 builtin toolbar controls that you've deleted. For more information, see the following section.
Restoring Builtin Toolbar Controls
You can restore builtin toolbar controls that you've deleted. However, you cannot restore custom toolbars or toolbar controls that you've deleted; you must recreate them.
Using the Customize Dialog Box
You can use the Customize dialog box to restore a builtin toolbar to its builtin set of controls.
To restore a builtin toolbar
Using Visual Basic
Use the Reset method to reset the components of a builtin toolbar.
The following Microsoft Excel example resets the Standard
toolbar to its default set of controls.
CommandBars("Standard").Reset
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 designtime changes to toolbars by using either the Customize dialog box or Visual Basic, you must use Visual Basic to make any runtime 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.
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.
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 builtin 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
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 runtime 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.
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 builtin 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 builtin 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 popup controls that display builtin menus. However, those popup controls don't contain the builtin menu items on those menus; that is, the popup 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 builtin 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 can assign the ID of a builtin 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 builtin 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 builtin menu items and toolbar controls in a specific Office application, you can do any of the following:
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
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 popup controls for builtin menus are in
the range 30002 to 30426. Remember that these IDs return empty
copies of the builtin menus.