Creating an application plan
In Chapter 2, I describe how to use pseudo-code to write a program. The pro-
gram in that chapter uses only one module because it’s quite simple. Not
every program that you create is that simple. Sometimes a program performs
complex tasks, and you need to create an
overview,
or
application plan,
first.
Think of the Lego approach again: Each block is separate, but you don’t get
the whole picture until you put the blocks together. The application plan
shows how to put the blocks together to get a particular result. You have
three sizes of blocks that you can use:
Projects
Modules, forms,
and class modules
Sub-procedures and functions
Think about the writing of a VBA program this way: You have a pile of
blocks, and you have a picture of what you want to create in your mind.
To make the creation a reality, you choose and add blocks that look like
they fit. You can choose standard blocks from this book, the VBA help
file,
or online sites, such as FreeVBCode.com at
http://www.
freevbcode.com/
.
You can use a number of techniques to create your application plan. I usually
start with a list of major tasks, such as printing a report or finding a word.
This isn’t pseudo-code. You’re not writing a procedure for VBA to follow.
All that you’re thinking about are the major tasks that VBA has to perform.
Maybe your program performs only one major task. In that case, you might
be able to get by with a single project, module, and sub-procedure, like in the
example in Chapter 2.
67
Chapter 3: Writing Structured VBA Programs
Understanding the Lego approach
I call the method that I describe in this chapter
the
Lego approach because most people are familiar
with this toy and it’s an easy way to explain good
programming technique. Some people use other
names for this kind of programming. You might
hear
it called
modular programming, for example,
because it relies on modules to hold individual
pieces of code. Don’t confuse all these terms with
object-oriented programming, which I describe
in Chapter 8.
08_046500 ch03.qxp 12/5/06 5:34 PM Page 67
Defining the project
You may never need to define more than one project for a program. This is espe-
cially true of Access
and Excel projects, in which everything needed to hold
the data appears in one file in most cases. You can also place everything that
you need into a single Word template if this is the only template that you
need and the program is appropriate for use with more than one document.
However, think about Word for a moment, and you discover something about
projects. Figure 3-9 shows a typical Project Explorer window setup. Notice
that this window contains three projects: Normal, Project (Document1), and
TemplateProject (LETTER).
Word always loads the Normal template. (This
template can have one of a
number of different extensions, including DOT, DOTX, and DOTM in Office
2007.) Older versions of Word always use the Document Template (DOT)
extension. Word 2007 can also use Microsoft’s new XML format (DOTX) or
macro-enabled format (DOTM). If you want to create macros for Word 2007,
the best file format to use for compatibility is the DOT file, but you get the
greatest flexibility and feature set by using the DOTM file. You can’t store
macros in the DOTX file.
You can also use a custom template. Custom templates contain special for-
matting and macros
for a particular document type, such as a letter. As with
the Normal template, custom templates come with DOT, DOTX, and DOTM
extensions. You can never store macros in a DOTX file.
Finally, you also have the document, which also counts as a project. A docu-
ment
can have a DOC, DOCX, or DOCM file extension. You can store macros
only in the DOC and DOCM file formats. In this case, I wrote a program for
the individual document that uses template-specific sub-procedures and
Do'stlaringiz bilan baham: