
In this example, use a question mark (?) to ask the VBAIDE to tell you a variable’s value.
To set a variable’s value in the Immediate window, type the statement just as you would in the
Code window. Figure 1-10 shows how it’s done.
■Note You can’t declare variables in the Immediate window. Also, a variable’s scope and valid VBA
commands in the Immediate window are equally important. Chapter 2 covers variable scope.
The Options Dialog Box
The Options dialog box, shown in Figure 1-11, lets you customize the IDE’s look and feel, in-
cluding syntax color scheme, source code font, and tab spacing. To open the Options dialog
box, choose Tools ➤Options. This section covers the most commonly used options.
For most developers, the default settings are fine. However, you should consider changing
two settings on the Editor tab: Auto Syntax Check and Require Variable Declaration.
When the Auto Syntax Check option is checked, syntax errors in your code generate an
error message similar to Figure 1-12.
CHAPTER 1 ■THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE)8
Figure 1-10. Setting a variable in Immediate window
Figure 1-11. The Options dialog box

As you begin to develop more complex applications and
reuse lines of code from other places in your application, these
error messages will become a nuisance. Any time you move the
cursor off the offending line of code, you get one of these error
messages. But if you uncheck Auto Syntax Check, the VBAIDE
notifies you of errors by changing the color of the offending
line of code to red.
The Require Variable Declaration option is unchecked by
default, meaning that the VBAIDE does not require that you
properly declare your variables before you use them. This isn’t
much of a concern when you write a simple macro, but when you start developing larger and
more complex applications, you’ll find this option indispensable. Checking this option forces
you to think about each variable and its data type. When you check this option, the VBAIDE
adds a line of code to the start of each module, as shown in Figure 1-13.
After you check this option, exit and restart AutoCAD to make it take effect.
By declaring variables to be a specific data type, you save memory resources. Undeclared
variables are, by default, assigned the variant data type. This data type uses the most memory
and could lead to memory resource problems when users run your application. As a rule of
thumb, always declare each variable you use in your application, and choose the data type that
uses the least possible memory. Chapter 2 discusses data types and memory in more detail.
Managing Projects
Managing your code components is critical to successfully developing applications. This
section discusses adding components to your project, saving your project, and loading and
executing an application.
Project Structure
A VBA project contains several different types of files, including the following:
•UserForm module
•Standard module
•Class module
• Reference .dvb file
CHAPTER 1 ■THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) 9
Figure 1-12. The error-
message dialog box
Figure 1-13. An example of the checked Require Variable Declaration

UserForm Module
UserForm modules (files with a .frm extension) contain a text description of your form, controls
placement, and property settings. They also include UserForm-level declarations of constants,
variables, and procedures; and event procedures.
Standard Module
Standard modules (files with a .bas extension) contain module-level declarations of programmer-
defined data types, constants, variables, and public procedures. A standard module typically
contains routines that don’t fit nicely into a class definition.
Class Module
Use class modules (files with a .cls extension) to create your own objects, including methods,
properties, and events. Class modules are similar to UserForm modules except that they have
a visible user interface. Class modules are very versatile and vital to VBA and AutoCAD. As you
progress through this book, you’ll see that classes and objects are everywhere.
Reference .dvb File
You can reference the code of another .dvb file in your current project. This feature lets you
easily reuse code among several projects. You can’t create a circular reference, which is a refer-
ence to one project and a reference in that project to the current project. If you accidentally
create a circular reference, AutoCAD tells you of the error. You have to undo the reference
before you can continue.
Creating, Opening, and Saving Projects
To extract, embed, create, save, load, and unload VBA projects, open the VBA Manager dialog
box, shown in Figure 1-14. To open it, either type VBAMAN at the AutoCAD command prompt
or choose Tools ➤Macros ➤VBA Manager.
You must explicitly load all .dvb projects. AutoCAD loads embedded projects automatically
when the drawing containing them is opened, depending upon how you configure AutoCAD’s
security options. Clicking New creates a new project in the VBAIDE that you can access by
clicking the Visual Basic Editor button. To load an existing project, click the Load button. The
Open VBA Project dialog box in Figure 1-15 appears, letting you choose the project to load.
■Tip Embedding VBA macros within drawings is fine for drawings that remain within your organization.
Avoid embedding macros when you’ll deliver the drawings to outside users or customers as it imposes
a security risk on their part to trust your macros in their environment.
This dialog box is similar to the standard File Open dialog box in Windows. Similar to cre-
ating a new project, when you choose the .dvb project you want, click the Visual Basic Editor
button to start working on your project.
CHAPTER 1 ■THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE)10

There are two other ways to create and load DVB project files:
• Type VBAIDE at the AutoCAD command prompt or press Alt+F11 to open or create a
DVB project file.
• Type VBALOAD at the AutoCAD command prompt to open the Open VBA Project dia-
log box so you can choose a project to load.
To save your project, choose File ➤Save or press Ctrl+S. If you have not previously saved
your project, the standard Save As dialog box appears, as shown in Figure 1-16.
CHAPTER 1 ■THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) 11
Figure 1-14. The VBA Manager dialog box
Figure 1-15. The Open VBA Project dialog box

Unlike in Visual Basic, you don’t need to save each project module separately. AutoCAD
saves them all in a .dvb file. However, as the next section illustrates, you can export each mod-
ule to a separate file.
■Tip Avoid saving your custom program files under the AutoCAD installation folder tree. Instead, create
a separate folder tree for them. This prevents AutoCAD installations and updates from affecting your
program files.
Adding, Saving, and Removing Files
You will sometimes want to add to your VBA project a file such as a common UserForm module
or a collection of routines in a standard module. To do this, choose File ➤Insert File or press
Ctrl+M. The Import File dialog box appears, as shown in Figure 1-17.
To export a module to a separate file, highlight the module name in the Project Explorer
window, and then either choose File ➤Export File or press Ctrl+E. A Save As dialog box
appears for the type of file to export. Alternatively, you can highlight the module name in the
Project Explorer window, right-click to invoke the pop-up menu, and choose Export File.
To remove a file from your project, highlight the module name in the Project Explorer dia-
log. Then choose File ➤Remove (you will be offered the option of exporting the file prior to
removal). You can also highlight the module name, right-click to invoke the pop-up menu in
Figure 1-18, and then choose Remove.
Notice that this menu also includes the Export File option.
CHAPTER 1 ■THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE)12
Figure 1-16. Save As dialog box

