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
5793c01_final.qxd 8/22/05 2:41 AM Page 8
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
5793c01_final.qxd 8/22/05 2:41 AM Page 9
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
5793c01_final.qxd 8/22/05 2:41 AM Page 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
5793c01_final.qxd 8/22/05 2:41 AM Page 11
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
5793c01_final.qxd 8/22/05 2:41 AM Page 12
Adding ActiveX Controls and Code Components
When you start a project and add a UserForm module, a common Toolbox appears. It contains a
standard collection of ActiveX controls called intrinsic controls. Chapter 3 covers intrinsic con-
trols in more detail. If you want to insert an ActiveX control that is not in the Toolbox, choose
Tools
➤ Additional Controls or right-click in the Toolbox window and choose Additional Con-
trols. The Additional Controls dialog box shown in Figure 1-19 appears.
This dialog box lists all the ActiveX controls that are properly registered on your machine.
However, if you want to use a particular ActiveX control, check its End User License Agree-
ment (EULA) to determine whether you have a license to use it in a VBA host application.
CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) 13
Figure 1-17. The Import File dialog box
Figure 1-18. The
Remove Module
pop-up menu
Figure 1-19. The Additional Controls dialog box
5793c01_final.qxd 8/22/05 2:41 AM Page 13
Many ActiveX controls installed with Microsoft Visual Basic, including TreeView, ListView,
File, Directory, and Drive, are not licensed for use in a VBA host application. However, Chap-
ter 19 explains some easy ways you can use the Windows application programming interface
(API) to get around this dilemma.
You can add more than ActiveX controls—code components are perhaps even more com-
mon. This is the means by which your application can gain access to other ActiveX automation
applications such as Microsoft Excel, Access, and Word. ActiveX automation is your key to
building powerful applications that take advantage of objects exposed by other applications.
To use the objects, methods, properties, and events that other applications expose, first
add a reference to that application. Choose Tools
➤ References to open the References dialog
box, shown in Figure 1-20.
Check the reference you want to add. The dialog box contains an alphabetical list of the
references your application doesn’t use. Later, when you write your own objects using Class
modules, this is where you’ll add them to your project.
To improve performance, deselect any references your application doesn’t use. Each refer-
ence must be resolved before your project loads. Depending on your project’s size, this could
greatly decrease how long your user waits for the application to load and run.
■Note If your application uses an object of another application, you can’t remove the reference to it
without first removing the object.
CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE)14
Figure 1-20. The References dialog box
5793c01_final.qxd 8/22/05 2:41 AM Page 14
The Object Browser
When you set a reference to an application’s object library, the Object Browser lists all the objects,
methods, properties, constants, and events that application exposes. To open the Object Browser,
choose View
➤ Object Browser or press F2. By default, the Object Browser lists all libraries your
project currently references. To view just the AutoCAD library, for example, click AutoCAD in the
drop-down list in the Object Browser, as shown in Figure 1-21. The Object Browser gives you a
perspective of the objects an application exposes and the methods, properties, events, and con-
stants those objects expose for your application to manipulate.
Loading and Running Applications
You can load and subsequently execute your VBA applications in many different ways. This
section explains the most common ways.
Acad.dvb
AutoCAD searches the support file search path for the file acad.dvb. If AutoCAD finds this file,
it loads it into the current session. The following example illustrates how to implement this
feature.
Public Sub Start()
Application.ActiveDocument.SetVariable "OSMODE", 35
End Sub
Place this code in the ThisDrawing module and save the file as acad.dvb. Save this file in
any subdirectory specified in the support file search path. Now each time you start an AutoCAD
session, AutoCAD loads this file. Also, if you include a routine called AcadStartup, AutoCAD exe-
cutes it when it loads acad.dvb.
The example in Figure 1-22 shows how to use the macros that you have stored in the
acad.dvb file.
CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) 15
Figure 1-21. The Object Browser dialog box
5793c01_final.qxd 8/22/05 2:41 AM Page 15
Type –vbarun then the name of the macro to run (Start in this instance). In this example,
the value of the system variable OSMODE is set to 35. This is a convenient way to store macros
that will invoke AutoCAD with a particular setup.
Acaddoc.lsp
AutoCAD automatically searches the default search path (a combination of the support/file path
list, the current working folder, and the shortcut’s startup folder) for a file named acaddoc.lsp. If
AutoCAD finds this file, it loads it into the current drawing. Unlike with acad.dvb, which loads
only when you start a new AutoCAD session, AutoCAD loads acaddoc.lsp each time you open or
create a drawing.
■Tip To verify the actual search-path list, go to the command prompt, type
-insert
, enter a meaningless
string of characters such as
sdfsdfsdf
(keep it less than 31 characters, though), and press Enter. AutoCAD
displays a list of folders in which it tried to find your file when it failed.
AutoCAD provides a special programmer-defined function called S::STARTUP that, if
included in acaddoc.lsp or any default startup LISP file, is automatically executed when you
open or create a drawing. You can define S::STARTUP in acaddoc.lsp to perform setup opera-
tions for each drawing.
You can define an S::STARTUP function in several different places, including acad.lsp,
acaddoc.lsp, a .mnl file, or any AutoLISP file loaded from any of these files. You can overwrite
a previously defined S::STARTUP function—which means that another definition can over-
write your S::STARTUP routine.
■Note Never modify or replace the files acad2006.lsp or acad2006doc.lsp, as they may be overwritten
by a service pack installation without warning. It is also a good idea to append the
S::STARTUP function in-
stead of defining it, as many third-party products rely upon
S::STARTUP to initialize their environments. If you
define a new S::STARTUP, you could disable some or all of another loaded product in the process.
The following example shows how to ensure that your startup function works with other
functions.
(defun-q Startup ()
(command "-vbarun" "Start")
)
(setq S::STARTUP (append S::STARTUP Startup))
CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE)16
Figure 1-22. The AutoCAD command prompt
5793c01_final.qxd 8/22/05 2:41 AM Page 16
This code appends your startup function to any existing S::STARTUP function, and then
redefines the S::STARTUP function to include your startup code. This works regardless of any
other existence of an S::STARTUP function.
■Note In AutoLISP, you must use defun-q as opposed to defun for this example to work properly. Visual
LISP constructs functions differently from AutoLISP between defun and defun-q.
Embedded Projects
AutoCAD lets you embed a VBA project into a drawing. Each time the drawing is loaded, AutoCAD
also loads the VBA project embedded in the drawing. Of all the options to load VBA projects auto-
matically, this is the worst one. It stores the VBA project with the drawing, making your drawing
file that much bigger. If you copy the drawing file to create a new drawing, you also copy its VBA
project. If you want to change the VBA project, you need to change each VBA project in every
drawing file that you created from the original. In addition, delivering drawings with embedded
macros to customers imposes a serious potential security risk on their part. Well, you get the pic-
ture of why this is the worst option to choose!
VBARUN and the Macros Dialog Box
The dialog-box version of the VBARUN command features several options that extend how you
create and execute a VBA macro.
You can execute a VBA macro at the AutoCAD command prompt. To execute a macro
from the AutoCAD command prompt, type –VBARUN, press Enter, and type the macro name,
similar to the example in Figure 1-23.
In this example, AutoCAD would execute the Start macro.
If the macro you wish to execute is unique among all the VBA projects loaded, then just
specify the macro name after the –VBARUN command. However, if you have multiple macros
loaded with the same name, specify the macro to execute using either of these syntaxes:
ProjectName.ModuleName.MacroName
or
ModuleName.MacroName
How far down inside your project and modules your macro is placed, and whether you
have multiple projects loaded determine which syntax is appropriate.
CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) 17
Figure 1-23. The AutoCAD command prompt
5793c01_final.qxd 8/22/05 2:41 AM Page 17