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 )
BeforeRightClick
Calculate
Change
Deactivate
PivotTableUpdate (Excel 10
only)
SelectionChange
Occurs when a worksheet is right-clicked, before the default rightclick action.
Occurs after the worksheet is recalculated.
Occurs when cells on the worksheet are changed by the user or by
an external link.
Occurs when the worksheet is deactivated.
Occurs after a PivotTable report is updated on a worksheet.
Occurs when the selection changes on a worksheet.
11.5 WorkBook Events
Table 11-2 shows the workbook-related events.
Table 11-2. Workbook Events
Event name
Description
Activate
Occurs when a workbook is activated.
AddinInstall
Occurs when the workbook is installed as an add-in.
AddinUninstall
Occurs when the workbook is uninstalled as an add-in.
BeforeClose
Occurs before the workbook closes.
BeforePrint
Occurs before the workbook (or anything in it) is printed.
BeforeSave
Occurs before the workbook is saved.
Deactivate
Occurs when the workbook is deactivated.
NewSheet
Occurs when a new sheet is created in the workbook.
Open
Occurs when the workbook is opened.
PivotTableCloseConnection (Excel 10 Occurs after a PivotTable closes the connection to its data
only)
source.
PivotTableOpenConnection(Excel 10 Occurs after a PivotTable opens the connection to its data
only)
source.
SheetActivate
Occurs when any sheet is activated.
Occurs when any worksheet is double-clicked, before the
SheetBeforeDoubleClick
default double-click action.
Occurs when any worksheet is right-clicked, before the
SheetBeforeRightClick
default right-click action.
Occurs after any worksheet is recalculated or after any
SheetCalculate
changed data is plotted on a chart.
Occurs when cells in any worksheet are changed by the
SheetChange
user or by an external link.
SheetDeactivate
Occurs when any sheet is deactivated.
Occurs when the selection changes on any worksheet
SheetSelectionChange
(does not occur if the selection is on a chart sheet).
WindowActivate
Occurs when any workbook window is activated.
WindowDeactivate
Occurs when any workbook window is deactivated.
WindowResize
Occurs when any workbook window is resized.
115
Incidentally, a user can suppress the Open event for a workbook by holding down the Shift key
when opening the workbook.
11.6 Chart Events
Table 11-3 shows the chart-related events.
Table 11-3. Chart Events
Event name
Description
Activate
Occurs when a chart sheet or embedded chart is activated.
Occurs when an embedded chart is double-clicked, before the default doubleBeforeDoubleClick
click action.
Occurs when an embedded chart is right-clicked, before the default right-click
BeforeRightClick
action.
Calculate
Occurs after the chart plots new or changed data.
Deactivate
Occurs when the chart is deactivated.
DragOver
Occurs when a range of cells is dragged over a chart.
DragPlot
Occurs when a range of cells is dragged and dropped on a chart.
MouseDown
Occurs when a mouse button is pressed while the pointer is over a chart.
MouseMove
Occurs when the position of the mouse pointer changes over a chart.
MouseUp
Occurs when a mouse button is released while the pointer is over a chart.
Resize
Occurs when the chart is resized.
Select
Occurs when a chart element is selected.
SeriesChange
Occurs when the user changes the value of a chart data point.
11.7 Application Events
Table 11-4 shows the Application-level events. These events apply to all objects in the currently
running instance of Excel.
Table 11-4. Application Events
Event name
Description
NewWorkbook
Occurs when a new workbook is created.
SheetActivate
Occurs when any sheet is activated.
Occurs when any worksheet is double-clicked,
SheetBeforeDoubleClick
before the default double-click action.
Occurs when any worksheet is right-clicked, before
SheetBeforeRightClick
the default right-click action.
Occurs after any worksheet is recalculated or after
SheetCalculate
any changed data is plotted on a chart.
Occurs when cells in any worksheet are changed by
SheetChange
the user or by an external link.
Occurs after the sheet containing the PivotTable
SheetPivotTableUpdate (Excel 10 only)
report has been updated.
116
Occurs when any sheet is deactivated.
Occurs when the selection changes on any
SheetSelectionChange
worksheet (does not occur if the selection is on a
chart sheet).
WindowActivate
Occurs when any workbook window is activated.
WindowDeactivate
Occurs when any workbook window is deactivated.
WindowResize
Occurs when any workbook window is resized.
WorkbookActivate
Occurs when any workbook is activated.
WorkbookAddinInstall
Occurs when a workbook is installed as an add-in.
WorkbookAddinUninstall
Occurs when any add-in workbook is uninstalled.
Occurs immediately before any open workbook
WorkbookBeforeClose
closes.
WorkbookBeforePrint
Occurs before any open workbook is printed.
WorkbookBeforeSave
Occurs before any open workbook is saved.
WorkbookDeactivate
Occurs when any open workbook is deactivated.
Occurs when a new sheet is created in any open
WorkbookNewSheet
workbook.
WorkbookOpen
Occurs when a workbook is opened.
WorkbookPivotTableCloseConnection
Occurs after a PivotTable report connection has
(Excel 10 only)
been closed.
WorkbookPivotTableOpenConnection (Excel Occurs after a PivotTable report connection has
10 only)
been opened.
AM
FL
Y
SheetDeactivate
Unfortunately, Excel makes it a bit more difficult to reach the Application events than events in
the other categories. Here is a step-by-step procedure for reaching the event code shells for the
Application events:
TE
1. Use the VBA Insert menu to insert a class module into your project. Let us call this class
module CApp (short for Class Application). In the declaration section of the class
module, add the line:
Public WithEvents App As Application
Choosing the App object in the objects drop-down should now give you access to the
Application event code shells, as shown in Figure 11-2.
Figure 11-2. Application-level events
2. In the code module in which you want to activate Application-level events (say, the code
module associated with a workbook, worksheet, or chart), place the following declaration
in the declarations section of the module:
117 ®
Team-Fly
Dim AppObj As New CApp
(You can use any variable name you wish in place of AppObj ).
3. Finally, assign the App property of AppObj to the Application object, by executing the
code:
Set AppObj.App = Excel.Application
It is up to you where to place this line of code, but it must be executed in order to activate
Application-level events. (There is a certain circularity here, since a natural place to put
this code is in the WorkbookOpen event. However, this event will not fire until this code
has been executed.)
In addition to using the EnableEvents property, you can turn off Application-level events by
executing the code:
Set AppObj.App = Nothing
11.8 QueryTable Refresh Events
Table 11-5 shows the events related to QueryTables. We will not discuss QueryTables in this book,
but at least now you are aware of the existence of these events should you decide to pursue this
matter on your own.
Event name
AfterRefresh
BeforeRefresh
Table 11-5. Refresh Events
Description
Occurs after a query is completed or canceled.
Occurs before any refreshes of the query table.
118
Chapter 12. Custom Menus and Toolbars
In this chapter, we discuss methods for programmatically controlling menus and toolbars. Even
though the subject of menus and toolbars is fairly straightforward, it can seem very confusing,
especially since the documentation is less helpful than it might be.
12.1 Menus and Toolbars: An Overview
Actually, Excel's menu and toolbar objects do not belong to the Excel object model. The menus
and toolbars throughout the Microsoft Office application suite belong to the Office object model.
The portion of the Office object model that relates to menus and toolbars is shown in Figure 12-1.
Figure 12-1. The menu and toolbar portion of the Office object model
Note that this model is actually quite small, containing only two objects and their corresponding
collections:
•
•
CommandBar objects and the CommandBars collection
CommandBarControl objects and the CommandBarControls collection
12.1.1 Menu Terminology
To help set the notation, Figure 12-2 shows the components of the Office menu structure (this
happens to be a Word menu, but no matter).
Figure 12-2. An Office menu
119
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