Launching the VBA IDE
To get started, you’ll want to become familiar with the AutoCAD VBA Integrated DevelopmentEnvironment, or IDE. The following steps show you how to open the IDE.
1.
In AutoCAD, choose Tools
➢
Macro
➢
Visual Basic Editor to open the VBA editor in aseparate window.
2.
To move between the IDE and the AutoCAD window, click the View AutoCAD button atthe left end ofthe Editor toolbar, or choose View
➢
AutoCAD from an editor menu. Youcan also press Alt+F11 to move from the editor back to AutoCAD or to go from the mainAutoCAD window back to the VBA editor.
Tip
You can also open the editor automatically when you load a VBA project. To do so, choose Tools
➢
Macro
➢
Load Project and make sure the Open Visual Basic Editor check box is selected.
Creating a New Module
At any time, you can have more than one VBA project open in AutoCAD. (You do not need to haveany project open, however.) All the components in a project are loaded at the time the project isopened. A project can consist ofany ofthese components:
◆
Modules
◆
Class modules
◆
UserFormsA
module
is a container for VBA code. A
class module
is the VBA component used to define a newtype ofobject. A
UserForm
is a customizable user interface component. Class modules are anadvanced topic, and you’ll learn about UserForms in Bonus Chapter 6 (which is on the CD), but fornow, let’s just create a standard module. This will be a place where you can execute code samples.Ifyou have AutoCAD open with a default blank drawing loaded, you’ll find that this drawingincludes a default VBA project named ACADProject.
1.
To insert a new module, choose Insert
➢
Module, or click the Insert Module button on theStandard toolbar. This creates a new module and opens that module in the editor. The editor candisplay many modules at once, since it’s a Multiple Document Interface (MDI) application.
2.
While you’re learning VBA, you’ll probably find it useful to maximize the module you’reworking with. Click the Maximize button in the Module1 window to enlarge it.
Creating and Running a New Procedure
Modules hold VBA code, but one module can contain a lot ofcode. In VBA, code is broken into proce-dures. You’ll learn about the different types ofprocedures in a moment, but for now, let’s just create one.
1.
In the Module1 window, type
Sub HelloWorld
and press the Enter key. VBA inserts paren-theses at the end ofthe procedure definition and automatically creates an
End Sub
line tomark the end ofthe procedure.
2.
Now type
MsgBox “Hello World”
in between the
Sub
and
End Sub
lines. Notice that VBAprovides you with Quick Info about the arguments for the MsgBox function. For now, youcan just ignore these.
EXPLORING VBA
2
T
HISCHAPTERORIGINALLYAPPEAREDIN
M
ASTERING
A
UTO
CAD 2004
AND
A
UTO
CAD LT 2004
ANDHASBEENUPDATEDFOR
A
UTO
CAD 2005
AND
A
UTO
CAD LT 2005.
Add a Comment