Visual Basic for Applications (VBA) is integrated as a standard part of AutoCAD 2000. This means that you can use VBAfor all of your AutoCAD development tasks, instead of (or in addition to) ObjectARX and AutoLISP. If you’re already familiar with some dialect of BASIC, you’ll find that VBAis easy to learn. If you’re a seasoned AutoLISP developer, you may find the VBAway of doing things to be odd, but after a while it will make sense. There’s a table at the end of this chapter that will help you translate AutoLISP concepts to their VBAequivalents
Getting Started with VBA
Microsoft’s VBApackage supplies two things to AutoCAD. First, there’s the VBA language engine itself, which does the job of interpreting any VBAcode you write when your application is executed. Second, there’s the VBA Integrated Development Environment (IDE), which provides you with the tools for editing and debugging your VBA code. In this chapter, you’ll learn the basics of the VBAlanguage itself. The next chapter describes the IDE and how to integrate VBAcode With the AutoCAD user interface. However, you need to know just enough about the IDE to type in and execute procedures to follow along with this chapter, so here’s a quick introduction.
Launching the VBA IDETo open the VBAeditor, select Tools >- Macro >- Visual Basic Editor from an AutoCAD drop-down menu. This opens the VBAeditor in a separate window. You can use the Windows taskbar to move back and forth between the editor and the main AutoCAD window. You can also move from the editor to the AutoCAD window by clicking the View AutoCAD button at the left end of the Editor toolbar, or by selecting View >- AutoCAD from an editor menu. The Alt+ Fll key combination also moves you from the editor back to AutoCAD. Pressing Alt+Fll again takes you from the main AutoCAD window back to the VBA editor
Creating a New Module
• Class modules
A module is a container for VBA code. A class module is the VBAcomponent to define a new type of object. A User Form is a customizable user component Class modules are an advanced topic, and you’ll learn about Forms in Chapter 99 (which is on the CD), but for now, let’s just create a standard module. This will be a place where yo~an execute code samples. If you have AutoCAD open .;ith a default blank drawing loaded, you’ll find that this drawing includes a default VBAproject named ACADProject. Toinsert a new module, select Insert >Module from the IDE menus, or click the Insert Module button on the Standard toolbar. This creases a new module and opens that module in the editor. The’ editor can ,ciisplay many modules at once, since it’s a Multiple Document Interface (MDI)’4pplication. While you’re learning VBA, you’ll probably find it useful to maximize the module you’re working with.
Creating and Running a New Procedure
Modules hold VBAcode, but one module can contain a lot of code. In VBA, code is broken up into procedures. You’ll learn about the different types of procedures in a moment, but for now, let’s just create one. In the Modulel window, type Sub HelloWorld and press the Enter key. VBA will insert parentheses at the end of the procedure definition, and automatically create an End Sub line to mark the end of
the procedure. Now type MsgBox “Hello World” in between the Sub and End Sub lines. Notice tha~VBAprovides you with Quick Info about the arguments for the MsgBox function. For now, you can just ignore these. Click the mo~e anywhere off the line you.’ve just typed to accept it and dear the Quick Info, Now that you’ve created a procedure, let’s run it. You can run procedures interactively in the VBAenvironment by using the Immediate window (sometimes called
the Debug window). This window may already be displayed at the bottom of the IDE. If not, you can open it using View >Immediate Window or the Ctrl+G keystroke combination.
Type HelloWorld into the Immediate window to tell VBA to execute the HeWorld procedure that\you just created the MsgBoxstatement creates a popup message your AutoCAD window (and, incidentally, makes the AutoCAD window the active window. Click OK in this dialog box to return to the VBA IDE. If you’ve followed along with the instructions in this section, your screen will look like.
Your VBA code is stored in procedures, which themselves are grouped into modules. Procedures come in two flavors: Function procedures, and Sub procedures, usually just called Functions and Subs. In this tection, you’ll learn how to create and call both types of procedures and how to pass information back and forth between procedures.
Subs and Functions
The difference between Subs and Functions is that Functions return a value to the caller, while Subs do not.
While there is an Insert> Procedure item on the VBAmenu, it’s really unnecessary. That’s because, as you saw previously for a Sub, it’s simpler just to type in the Sub or Function keyword, name the procedure, and let VBA do the rest. As illustrated in the following example, you can create one of each type of procedure by typing only four words into a module (the boldface statements are the parts
you need to type; VBAwill supply the rest
The ShowSquare and ReturnSquare procedures both square a number provided by the user. To do this, you need to equip each procedure with an argument. An argument is a placeholder for information that will be supplied at runtime. Let’s also add the code to these two procedures to make them deliver the square. Here’s the modified version:
In each of these procedures, x is an argument. At runtime (that is, when you actually execute the procedure) you must supply a value for the argument. shows the Immediate window after executing each of these procedures.
Note that the calling syntax differs between Fuitctions and Subs. For a Sub, you simply supply the name of the procedure, followed by any arguments the Sub takes, without parentheses. To can a Punctiocl from the Immediate window, you use the ? operator (think of this operator as meaning “What is the value of?”) followed by any arguments the Function takes, surrounded by parentheses. . Although both procedures proVide the output in the sample, they use different methods to provide that output. The Sub calls Debug.Print, which is a special bit of VBA syntax that takes whatever follows it on the line and prints it to the Immediate window. The Function aasigJls the result to its own name. This has the effect of making that the return value of the Function. Of course, one procedure can call other procedures. In the following example, the CallBoth procedure executes both of the other two procedures:
Here, y is a local variable: a place to store a bit of ihformation (you’ll learn more about variables in the next section). To call a.function procedure from within another procedure, you assign the value of the .Function procedure to a local variable .
Optional and Nam d Arguments
VBA supports two types of argument: optioaal and named (actually, every argument in VBA is a named argument). Here a Function definition that uses both:
Function Increment(Original, Optional AMount = 1)
Increment· Original + AMount
In this example, both Original and Amount are named arguments. In addition, Amount is an optional argument, with a default value of 1. You can execute the Increment procedure from turn: Imtnediate window in a variety of ways:’
The first example treats the arguments positionally, supplying a Value for each one in order. The second example does the same, but does not supply a value for the Amount argument. In this case, the Am~t argument defaults toI, and the result of the Function is 6 (try it yourself!). The third example also ‘ the optional argument, but uses thenamed argument syntax of Name:-Val ue to make it clear what information is being supplied. The f~ and fifth exemples show that .you can supply arguments in whatever order you like if you’re using named arguments.