Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.72 MB, 490 trang )
12.1.2 The CommandBar Object
Toolbars, menu bars, menus, submenus, and shortcut menus are all CommandBar objects. (A
shortcut menu is a menu that pops up in response to a right mouse click.) Thus, every item
pictured in Figure 12-2 is a command bar except the popup controls and the button control.
Of course, toolbars, menu bars, and shortcut menus are "top level" objects, whereas menus and
submenus emanate from toolbars, menu bars, or shortcut menus.
It is important to note that Office VBA does not treat each of these CommandBar objects in the
same way. For instance, the Count property of the CommandBars collection counts only the toplevel items: menu bars, toolbars, and shortcut menus. It does not count menus or submenus. Also,
the Add method of the CommandBars collection can be used to create toolbars or menu bars, but
not menus or submenus.
The CommandBar object has a Type property that can assume one of the constants in the
following enum:
Enum MsoBarType
msoBarTypeNormal = 0
msoBarTypeMenuBar = 1
msoBarTypePopup = 2
End Enum
' toolbar
' menu bar
' menu, submenu, or shortcut menu
12.1.3 Command-Bar Controls
The items on a toolbar, menu bar, menu, or submenu are actually controls, called command-bar
controls; that is, they are CommandBarControl objects. As we will see, there are various types of
command-bar controls, falling into two broad categories: custom command-bar controls
(including custom text boxes, drop-down list boxes, and combo boxes) and built-in command-bar
controls. Note that command-bar controls are not the same as the controls that we can place on a
UserForm; they are designed specifically for toolbars and menus.
There are two special types of custom command-bar controls that are not typical of other types of
controls. These are Popup controls and Button controls.
120
12.1.3.1 Popup controls
A command-bar control of type msoControlPopup is a control whose sole purpose is to pop up
a menu (when the control is on a menu bar) or a submenu (when the control is on a menu). These
controls are naturally referred to as popup controls (see Figure 12-2). Popup controls that are
located on a menu bar take on the appearance of a recessed button when the mouse pointer is over
the control. Popup controls on a menu or submenu have a small arrow on the far right to identify
them.
Thus, the term popup is used in two different ways. A popup control is a command-bar control of
type msoControlPopup and is used to pop up a menu or submenu. A popup command bar is a
command bar of type msoBarTypePopup and is either a menu, submenu, or shortcut menu. Note
that to display a popup command bar, the user needs to activate a popup control.
12.1.3.2 Button controls
A command-bar control of type msoControlButton is called a button control. When a button
control is activated (using an accelerator key or mouse click), a macro is executed. Button controls
have a string property called OnAction, which we can set to the name of the macro that is
executed when the control is activated.
12.1.4 Adding a Menu Item
It is worth mentioning now that there are a few counterintuitive wrinkles in the process of menu
creation. In particular, we might think at first that adding a new menu should be done using the
Add method of the CommandBars collection, specifying the name of the parent menu and the
location of the new menu on the parent. After all, a menu is a CommandBar object, and this
procedure would be consistent with other cases of adding objects to a collection.
However, this is not how it is done. Instead, as we will see, a new menu (or submenu) is created
by adding a command-bar control of type msoControlPopup to the CommandBarControls
collection of the parent menu (and specifying the new control's position on the parent). Actually,
this represents a savings of effort on our behalf. For, as we have remarked, a menu or submenu
requires a popup control for activation. Thus, Microsoft makes the task of creating menus and
submenus easier by automatically creating the corresponding (empty) menu or submenu in
response to our creation of a popup control. (We will see an example of this later, so don't worry
too much if this is not perfectly clear yet.)
One word of advice before proceeding: As we will see, when creating a new toolbar or menu, you
can set one of the parameters to make the object temporary, meaning that it will be destroyed
when Excel is closed. In this way, if anything unexpected happens, it is easy to recover—just
close Excel and reopen it. Alternatively, by opening the Customize dialog box (from the Tools
menu), you can delete menu items by dragging them off of the menu, and you can delete toolbars
by using the Delete button.
12.2 The CommandBars Collection
The topmost object that relates to menus and toolbars is the CommandBars collection, which
contains all of the application's CommandBar objects. The CommandBars collection is accessible
through the CommandBars property of the Application object, that is:
Application.CommandBars
121
The code in Example 12-1 will print a list of all of the CommandBar objects to the immediate
window. You may be surprised at the large number of objects, most of which are not currently
visible.
Example 12-1. Listing Excel's CommandBar Objects
Public Sub ShowCmdBars()
Dim sType as string, cbar as CommandBar
For Each cbar In Application.CommandBars
Select Case cbar.Type
Case msoBarTypeNormal
' A toolbar
sType = "Normal"
Case msoBarTypeMenuBar
' A menu bar
sType = "Menu bar"
Case msoBarTypePopup
' Menu, submenu
sType = "Popup"
End Select
Debug.Print cbar.Name & "," & sType & "," & cbar.Visible
Next
End Sub
If you execute this code, you should get the following entries, among many others:
Worksheet Menu Bar,Menu bar,True
Chart Menu Bar,Menu bar,False
This indicates that Excel's main menu bars are different for worksheets than for chartsheets, as is
evident if you look at the menus themselves. The worksheet menu bar has different controls than
the Chart menu bar. Thus, if you want to add a custom menu item to Excel's "main" menu bar,
regardless of what type of sheet is currently active, you will need to do so for both the Worksheet
Menu Bar and the Chart Menu Bar.
There is a slight complication concerning the CommandBars property that we should discuss.
When qualified with the Application object, as in Application.CommandBars, this property
returns the collection of all available built-in and custom command bars for the application which
in this case is Excel. This is why we used the fully qualified expression
Application.CommandBars in Example 12-1. Note that from a standard code module, we
can skip the qualification and just write CommandBars.
However, from a Workbook, the CommandBars property returns a different collection. In
particular, there are two possibilities. When the workbook is embedded within another application
and Excel is activated by double-clicking on that embedded workbook, the CommandBars
collection returns the collection of command bars that are available in that setting. This may be
different from the full collection of Excel command bars. If the workbook is not embedded in
another application, then the CommandBars property returns Nothing.
Note also that the Workbook object has a CommandBars property. However, this property is
meaningful only when the workbook is embedded within another application, in which case the
property returns the CommandBars collection for that application. When applied to a
nonembedded workbook, the property returns Nothing. Moreover, there is no programmatic way
to return the set of command bars attached to a workbook.
122
12.3 Creating a New Menu Bar or Toolbar
As we have said, one way in which menu bars and toolbars differ from menus and submenus is in
their creation. To create a new menu bar or shortcut menu, we use the Add method of the
CommandBars collection. The syntax for the Add method is:
CommandBarsObject.Add(Name, Position, MenuBar, Temporary)
The optional Name parameter is the name of the new command bar. If this argument is omitted,
Excel VBA assigns a default name (such as "Custom 1") to the command bar. The optional
Position parameter gives the position of the new command bar. This can be set to
msoBarLeft, msoBarTop, msoBarRight, msoBarBottom , msoBarFloating (for a
floating command bar), or msoBarPopup (for a shortcut menu).
The optional Boolean MenuBar parameter is set to True for a menu bar and False for a toolbar.
The default value is False, so if the argument is omitted, a toolbar is created. Note that if you
create a new menu bar and make it visible, it will replace the existing Excel menu bar! If this
happens, you can still exit Excel by typing Alt-F4, and the normal Excel menu will reappear the
next time that you launch Excel.
Setting the optional Temporary parameter to True makes the new command bar temporary.
Temporary command bars are deleted when Excel is closed. The default value is False.
To illustrate, the following code creates a new floating toolbar called "Custom Toolbar" and
makes it visible:
Dim cbar As Office.CommandBar
Set cbar = Application.CommandBars.Add("Custom Toolbar", _
msoBarFloating, False, True)
cbar.Visible = True
It is important to note that, if a CommandBar object by the name Custom Toolbar already exists,
the previous code will produce a runtime "Invalid procedure call" error. Thus, we really should
test for the existence of the CommandBar object before using the Add method, as shown in
Example 12-2.
Example 12-2. Creating a New Toolbar
Public Sub CreateToolbar()
Dim cbar As Office.CommandBar
Dim bExists As Boolean
bExists = False
For Each cbar In Application.CommandBars
If cbar.Name = "Custom Toolbar" Then bExists = True
Next
If Not bExists Then
Set cbar = Application.CommandBars.Add("Custom Toolbar", _
msoBarFloating, False, True)
cbar.Visible = True
End If
End Sub
123