/  28
 
Bonus Chapter 5
Exploring VBA
Visual Basic
for Applications (VBA) is integrated as a standard part of AutoCAD 2005. Thismeans that you can use VBA for all your AutoCAD development tasks instead of(or in additionto) ObjectARX and AutoLISP. Ifyou’re already familiar with some dialect ofBASIC, you’ll findthat VBA is easy to learn. Ifyou’re a seasoned AutoLISP developer, you may find the VBA way of doing things odd, but after a while it will make sense. A table at the end ofthis chapter will helpyou translate AutoLISP concepts to their VBA equivalents.Topics include the following:
Getting Started with VBA
Using Procedures
Variables and Constants
Control Structures
Built-in Functions
Converting from AutoLISP to VBA
IfYou Want to Experiment…
Getting Started with VBA
Microsoft’s VBA package supplies two things to AutoCAD. First, there’s the VBA language engineitself, which does the job ofinterpreting any VBA code you write when your application is exe-cuted. Second, there’s the VBA Integrated Development Environment (IDE), which provides youwith the tools for editing and debugging your VBA code. In this chapter, you’ll learn the basics of the VBA language itself. Bonus Chapter 6, found on this book’s CD, describes the IDE and how tointegrate VBA code with the AutoCAD user interface. However, you need to know just enoughabout the IDE to type and execute procedures to follow along with this chapter, so here’s a quick introduction.
 
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.
 
3.
Click anywhere offthe line you just typed to accept it and clear the Quick Info.
4.
Now that you’ve created a procedure, let’s run it. You can run procedures interactively in theVBA environment by using the Immediate window (sometimes called the Debug window).This window may already be displayed at the bottom ofthe IDE. Ifnot, you can open it bychoosing View
Immediate Window or by pressing Ctrl+G.
5.
Type
HelloWorld
in the Immediate window to tell VBA to execute the HelloWorld procedurethat you just created. The
MsgBox
statement creates a popup message in your AutoCAD window(and, incidentally, makes the AutoCAD window the active window, as shown in Figure 1).
6.
Click OK in this dialog box to return to the VBA IDE. Ifyou’ve followed along with theinstructions in this section, your screen will look like Figure 2.
Figure 2
The HelloWorldprocedure in theVBA IDE
Figure 1
The results of the HelloWorldprocedure
3
GETTING STARTED WITH VBA
 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.

Share & Embed

More from this user

Add a Comment

Characters: ...