Tải bản đầy đủ - 0 (trang)
Table 16-2. Members that return child objects

Table 16-2. Members that return child objects

Tải bản đầy đủ - 0trang

ReplaceFormat

Rows

RTD

Sheets

ShortcutMenus

SmartTagRecognizers

Speech

SpellingOptions

ThisCell

ThisWorkbook

Toolbars

Union

UsedObjects

Watches

Windows

Workbooks

WorksheetFunction

Worksheets



CellFormat

Range

RTD

Sheets

Menu

SmartTagRecognizers

Speech

SpellingOptions

Range

Workbook

Toolbars

Range

UsedObjects

Watches

Windows

Workbooks

WorksheetFunction

Sheets



There are some points worth noting in Table 16-2. First, there are several members that begin with

the word "Active." It should come as no surprise that these members return the corresponding

currently active object. For instance, the ActiveSheet member returns the currently active

worksheet or chart, depending upon which is active at the time. (Note that there is no Sheet object.

Sheets are either worksheets or stand-alone charts. We will discuss this issue in detail in Chapter

18.)

Observe also that often the name of a member is the same as the name of the object that the

member returns. For instance, the AddIns property returns the AddIns collection, the Application

property returns the Application object, and the Windows property returns the Windows collection.

The notable exceptions to this rule are:





The ThisWorkBook property returns the Workbook object containing the currently

running code. One use of this property is in determining the location (complete path and

filename) of the workbook on the user's computer, which is done by writing:

ThisWorkbook.FullName







Several object properties, such as Cells, Columns, and Rows, return a Range object. This

is because there are no Cell, Column, or Row objects in the Excel object model. Instead,

each of these "objects" is actually a Range object. (Incidentally, a similar thing happens in

the Word object model. In particular, there are no Character, Word, or Sentence objects.

Rather, these are Range objects in the Word object model as well.)



16.1.2 Members that Affect the Display

There are several members that affect the display of certain items:

DisplayAlerts property (R/W Boolean)



169



When True, Excel displays various warning messages (such as a confirmation message

that precedes the deletion of a worksheet) while a macro is running. If you do not want a

macro to be disturbed, then set this to False:

Application.DisplayAlerts = False



The default value of this property is True.

DisplayCommentIndicator property (R/W Long)

This property affects the way that Excel indicates the presence of a comment in an

unselected cell. It can be any one of the constants in the following enum:

Enum XlCommentDisplayMode

xlCommentIndicatorOnly = -1

only

xlNoIndicator = 0

xlCommentAndIndicator = 1

and comment



' Display indicator

' Display

' Display indicator

'



itself



End Enum



Setting DisplayCommentIndicator to either xlCommentIndicatorOnly or

xlCommentAndIndicator sets the value of the DisplayNoteIndicator property

(described later in this section) to True, while setting DisplayCommentIndicator to

xlNoIndicator changes DisplayNoteIndicator to False.

DisplayFormulaBar property (R/W Boolean)

This property determines whether the formula bar is displayed. Its default value is True.

DisplayFullScreen property (R/W Boolean)

This property determines whether Excel is in full-screen mode. (Note that displaying

Excel in full-screen mode is not the same as maximizing Excel's application window.)

DisplayNoteIndicator property (R/W Boolean)

If this property is True (its default value) then cells containing notes display cell tips and

contain note indicators (which are small dots in the upper-right corner of a cell). Setting

DisplayNoteIndicator to False also sets DisplayCommentIndicator to xlNoIndicator,

while setting DisplayNoteIndicator to True sets DisplayCommentIndicator to

xlCommentIndicatorOnly.



16.1.3 Members that Enable Excel Features

Several Application members enable or disable certain Excel features:

AutoFormatAsYouTypeReplaceHyperlinks property (R/W Boolean)

Set to True to have Excel automatically format hyperlink text as a hyperlink. Set to

False to turn off this often-annoying feature of Excel.

EnableAnimations property (R/W Boolean)



170



This property determines whether animated insertion and deletion is enabled. When

animation is enabled, inserted worksheet rows and columns appear slowly and deleted

worksheet rows and columns disappear slowly. The default value is False.

EnableAutoComplete property (R/W Boolean)

This property determines whether Excel's AutoComplete feature is enabled; its default

value is True.

EnableCancelKey property (R/W Long)

This property controls how Excel handles the Ctrl -Break or Esc key combinations during

a running procedure. It can be one of the following XlEnableCancelKey constants:

Enum XlEnableCancelKey

xlDisabled = 0

xlInterrupt = 1

xlErrorHandler = 2

End Enum



The meanings of these constants follow:

xlDisabled

Trapping is disabled (the keystrokes are ignored).

xlInterrupt

The running procedure is interrupted by the display of a dialog box that enables the user

to either debug or end the procedure. This is the default value.

xlErrorHandler

The keystroke interrupt is sent to the running procedure as an error that is trappable by an

error handler using the On Error GoTo statement. The error code is 18.

Note that this property can be dangerous and should be used with great circumspection. In

particular, if you set the property to xlDisabled, then there is no way to interrupt an

infinite loop. Similarly, if you set the property to xlErrorHandler but your error

handler returns using the Resume statement, there is no way to stop nonself-terminating

code.

For these reasons, Excel always resets the EnableCancelKey property to xlInterrupt

whenever Excel returns to the idle state and there is no code running.

EnableEvents property (R/W Boolean)

This property is True (its default value) if events are enabled for the Application object.

(For more on this, see Chapter 11.)

EnableSound property (R/W Boolean)

This property enables and (mercifully) disables sounds for Microsoft Office. The default

value is False.



171



16.1.4 Event-Related Members

It is possible to assign macros to certain events. (These are special events—not the events that we

discussed in Chapter 11.) For instance, we can assign a macro to play whenever a particular key is

pressed. This is done by invoking the OnKey method for the Application object. Let us describe

two of the more useful events that can be assigned a macro.

16.1.4.1 OnKey method

The syntax for the OnKey method is:

Application.OnKey(Key, Procedure)



where Key is the key or key combination (written as a string) that will execute the macro and

Procedure is the name of that macro.

Note that we can alter the normal behavior of Excel by assigning a key combination to the Key

parameter that has a normal Excel response (such as Ctrl-S for save). If we assign an empty string

to the Procedure parameter, then Excel will omit its normal response (so nothing will happen).

If we omit the Procedure parameter, then Excel will return the key combination to its normal

function.

To illustrate, the following code will disable the Ctrl-o key combination, which normally displays

the Open dialog box:

Application.OnKey "^o",""



The following code returns the Ctrl-o key combination to its normal Excel function:

Application.OnKey "^o"



The Key argument can specify a single key or any key combined with one or more of Alt, Ctrl, or

Shift. Normal alphanumeric keys are denoted by themselves, as in "a," "A," "1." Table 16-3 shows

how to enter special keys. For instance, the F2 key is denoted by "{F2}", and the Enter key is

denoted either by "{ENTER}" or "~".

Table 16-3. Special Keys for the Key Parameter

Key

Code

Backspace

{BACKSPACE} or {BS}

Break

{BREAK}

Caps Lock

{CAPSLOCK}

Clear

{CLEAR}

Delete or Del

{DELETE} or {DEL}

Down Arrow

{DOWN}

End

{END}

Enter (numeric keypad)

{ENTER}

Enter

~ (tilde)

Esc

{ESCAPE} or {ESC}

Help

{HELP}

Home

{HOME}

Ins

{INSERT}



172



Left Arrow

Num Lock

Page Down

Page Up

Return

Right Arrow

Scroll Lock

Tab

Up Arrow

F1 through F15



{LEFT}

{NUMLOCK}

{PGDN}

{PGUP}

{RETURN}

{RIGHT}

{SCROLLLOCK}

{TAB}

{UP}

{F1} through {F15}



To combine keys with Shift, Ctrl, or Alt, use the following prefixes:

Shift

Ctrl

Alt



+ (plus sign)

^ (caret)

% (percent sign)



For instance, to denote the Alt-F2 key combination, write "%{F2}". To denote Ctrl-Shift-Enter,

write "^+{ENTER}".

In order to use one of the characters +, ^, %, {, }, or ~ without having it interpreted as a special

key, simply enclose the character in braces. For instance, to reassign the { key, we would assign

the Key parameter to "{{}".

16.1.4.2 OnTime method

This method is used to run a procedure at a specific time or after a specific amount of time has

passed. The syntax is:

Application.OnTime(EarliestTime, Procedure, LatestTime, Schedule)



Of course, the Procedure parameter is the name of the macro to run. The EarliestTime

parameter is the time you want the macro to be run. To specify a time, we use the TimeValue

function. For instance, the following code executes the macro test in the ThisWorkbook code

module of the book1 workbook at 3:58 P.M.:

Application.OnTime TimeValue("3:58 PM"), _

"d:\excel\book1.xls!ThisWorkbook.test"

LatestTime is an optional parameter that specifies the latest time at which the procedure can

begin running. We can use the TimeValue function to specify a time for this parameter, or we

can set LatestTime to EarliestTime plus some additional time. For instance, the following



code requires that Excel run the macro no later than 30 seconds following 3:58 P.M.:

Application.OnTime TimeValue("3:58 PM"), _

"d:\excel\book1.xls!ThisWorkbook.test", _

TimeValue("3:58 PM") + 30



The LatestTime parameter may be useful, since if Excel is busy (running another procedure, for

instance), then execution of the macro denoted by Procedure will be delayed. If you do not

want the macro to be run after a certain time, then set the LatestTime parameter.



173



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Table 16-2. Members that return child objects

Tải bản đầy đủ ngay(0 tr)

×