enough to hold the number required to express the amount of available drive
space. Office 2007 has updated variables wherever the old variable isn’t large
enough to hold the expected data values. Consequently, you might find that
some of your old macros have broken because the data type has changed.
Because the drive doesn’t
have enough space, the code displays an error
message. The message shown in the code might seem long and complicated,
but it provides all the information needed to make a decision. It tells what’s
wrong, provides you with a choice of actions,
and contains specifics about
the amount of space needed. Notice the use of the
Format
function for the
message. It shows how to use a custom formatting string to display the free
space with comma separators but without a decimal point.
If you say that you want
to fix the drive space error, the code presents a mes-
sage box that tells you to click OK when it’s okay to make the change again.
The code uses the
GoTo
statement to redirect execution to the drive space
check again.
On
the other hand, if you decide not to fix the error, the program tells you the
result of the action. It then exits the
Sub
without doing anything else. This
step is important. Otherwise, the program
attempts to save the file, and the
error occurs.
Understanding error handlers
Error handlers are your option of last resort in dealing with bugs. You use an
error handler to work with any errors that you can’t trap.
The important thing
to remember is that the error has happened — you can’t prevent it at this
point. The best that you can do is to display a message box saying that the
error happened and to tell whether your program can recover from the error.
When your program can’t recover, it should fail gracefully. However, failing
gracefully shouldn’t be a standard option — always
try to recover from the
error. A program that fails gracefully performs the following tasks:
The program doesn’t rely on the standard VBA message. It uses a
custom message that contains detailed failure information instead.
The user is always informed about the consequences of the failure, pos-
sible fixes, and whom to contact (when necessary).
The reporting information should also appear in the Windows Event Log
when necessary to ensure that the administrator sees the error. VBA
doesn’t support this feature directly.
If possible, the program includes you as part
of the reporting loop so
that you can keep track of bugs in your application.
143
Do'stlaringiz bilan baham: