Cutting and pasting in Object Browser
Whenever you find a method, property, or event that you want to use in
Object Browser, you can copy the information to the Clipboard by clicking
the Copy to Clipboard button (the one with a symbol
that looks like two doc-
uments) and then pasting that information directly into your application
code. Using this feature means not only that you type less code, but also that
you have fewer errors to consider.
Getting help in Object Browser
Sometimes the information at the bottom of the Object Browser display isn’t
enough to tell you about the element that you’re viewing. When this happens,
highlight the element that you want to know more about and press F1, and
VBA displays the help screen for that element.
32
Part I: An Overview of VBA
05_046500 ch01.qxp 12/5/06 5:33 PM Page 32
Chapter 2
Your First VBA Program
In This Chapter
Creating an application plan
Defining the steps to create an application
Using different methods to run your application
Getting and using code found in the help files
I
n Chapter 1, I show you how to work with the VBA Integrated Development
Environment (IDE).
In that chapter, I also show you how to use the
Immediate window to create one-line test programs. However, the programs
that you create by using the Immediate window aren’t the same as permanent
programs (because Immediate window programs aren’t permanent), and
you’d find it difficult to perform useful work with them.
This chapter shows you how to move from the Immediate window into the
Code window. The
Code window
is where you create programs of a lasting
nature — the kind that you can use to perform the same task more than once.
Because of the time required to write the code, it only
pays to create an appli-
cation that you can use more than once. The benefit of writing a program is
that you can perform a repetitive task quickly.
One trick that you can use is to get hold of pre-made code whenever you can.
I end this chapter by showing you one of the techniques that is used most
often — stealing the code directly from Microsoft. (You’ll see that it isn’t
really stealing, but, considering all you get for free, it’s a steal in a sense.)
The help files that come with VBA contain a lot of code that you can use in
a number of ways. Microsoft knows that some developers want to use the
code, so it tries to make the samples as flexible as possible.
06_046500 ch02.qxp 12/5/06 5:33 PM Page 33
Deciding What to Do
Whenever
you decide to create a program, start with a plan. Just like a
builder needs a plan to construct a house, you need a plan to construct your
program. You can easily tell whether a builder decided to build a house with-
out using a plan, and it’s just as easy to determine when someone writes a
program without using a plan. Application users
can see that the application
isn’t well designed because it doesn’t work as anticipated. The plan that you
use doesn’t have to be very complicated, but you do need to think about
these questions:
What will the program do?
How will the program accomplish its task?
When will the program run?
Who will use the program?
Why is the program important?
Professional developers use a number of complex and hair-raising methods
to answer these questions. You work on much smaller programs, and you can
normally answer the questions quite easily. Don’t
make this more compli-
cated than you need to. You might answer the first question by saying, for
example, that the program will count the number of words in a document.
The reason that you want to go through this planning process is to ensure
that you’ve thought about the program you want to create. It’s easier to
answer the questions
before
you write any code than to fix the code later.
Writing down your answers also helps you avoid making the program into
something that you didn’t intend. This problem is a common one for every-
one; even developers with a lot of experience
write programs that quickly
grow beyond the original intent.
Another good reason to go through this planning process is to ensure that
you actually need to write the program. By describing a program in unam-
biguous terms, you can seek help from other people. In many cases, you’ll
find that you won’t need to write the application because
Someone else has already written the program.
You can obtain the program from a third-party vendor for less money
than it would require for you to write the application.
An update to the existing application includes the functionality of the
program as a feature.
34
Do'stlaringiz bilan baham: