722 AutoCAD Commands for VBA
If the check box is selected, the Visual Basic Editor is launched if it isn’t already. If the Visual Basic editor is minimized in the taskbar, it is restored to a normal display state.
5.Click the Open button to load the project.
6.In the AutoCAD message box, click Enable Macros.
By default, an AutoCAD message box (see Figure 4-2) is displayed when you load a VBA project. This message box informs you that the project could contain viruses or unsafe code. Clicking More Info loads the AutoCAD Online Help system.
Figure 4-2:
Warning message about potentially unsafe macros.
The other two options at the bottom of the message box — Disable Macros and Do Not Load — can be helpful if you are not sure who created the macros contained in the project. Disable Macros allows you to view the project in the editor, but code that would normally be run automatically is disabled. Do Not Load keeps the project from being loaded into the editor altogether.
If Open Visual Basic Editor was checked in the Open VBA Project dialog box, the Visual Basic Editor (see Figure 4-3) is displayed.
VBAUNLOAD
VBAUNLOAD is used to unload a VBA project from AutoCAD that was previously loaded and is no longer needed. This can help to free up system resources for other tasks. Unlike VBALOAD, which displays a dialog box or can be run from the command line, VBAUNLOAD can be run only from the command line.
After you are done working with or using a project, you can unload it without exiting AutoCAD. The following procedure describes how:
1.At the command line or dynamic input tooltip, type VBAUNLOAD.
2.At the Unload VBA Project prompt, type the full path to the project (the path from where it was loaded, plus the file name).
AutoCAD Commands for VBA 723
Figure 4-3:
The Visual
Basic Editor.
As an example, if the project Drawline.DVB was loaded from the folder C:\Program Files\AutoCAD 2006\Sample\VBA, you would enter C:\Program Files\AutoCAD 2006\Sample\VBA\Drawline.DVB at the prompt.
VBARUN
VBARUN is used to execute a publicly defined procedure that is contained in a code module found in a VBA project. The command launches the Macro dialog box, which allows you to do a variety of tasks from running a macro to changing some of the VBA environment options and even access to the Visual Basic Editor. After the procedure has been executed, you follow any prompts or respond to any dialog boxes that are displayed.
VBARUN can be used two different ways. The two different ways are controlled by whether the command’s name is prefixed with a hyphen. Using VBARUN with no hyphen launches the Macros dialog box. If -VBARUN is used, the prompt Macro Name is displayed at the command line or dynamic input tooltip.
Using -VBARUN can be great for calling a macro from a custom button on a toolbar or pull-down menu item. The following format shows how to use -VBARUN from the command line or dynamic input tooltip, or as part of a menu macro:
Book X
Chapter 4
Basic VisualAutoCAD
for
724 AutoCAD Commands for VBA
Macro name: <ACADProjectName>!<ModuleName>.<ProcedureName> “C:/Program Files/AutoCAD 2006/Sample/VBA/drawline.
dvb!Module1.drawline”
After a project has been loaded, you can execute one of the publicly defined procedures in a loaded project from AutoCAD, but not one that is defined as a private procedure. Here’s how:
1.At the command line or dynamic input tooltip, type VBARUN or from the Tools menu, choose Macro Macros.
The Macros dialog box (see Figure 4-4) appears.
Figure 4-4:
The Macros dialog box.
2.Select the macro (or procedure) name from the list box in the middle of the dialog box.
The list box displays all the macros of the loaded projects. This list may be very large because it is based on the number of projects that are loaded into AutoCAD at a given time. You can use the Macros In dropdown list to control which macros by project are listed in the list box.
3.Click the Run button on the right side of the dialog box.
VBAMAN
VBAMAN is used to launch the VBA Manager dialog box (see Figure 4-5). The VBA Manager dialog box offers a lot of functionality in a single dialog box. From this dialog box, you can load a project, unload a project, run a macro,
VBASTMT: Allows for executing a single line of VBA code at the dynamic tooltip or command line. It can integrate some simple VBA code into AutoLISP. This can be great for adding message boxes and some light functionality that isn’t part of AutoLISP.
Working with the IDE
The tool that is used to develop VBA projects is referred to as the Integrated Development Environment (IDE). The IDE is loaded by running VBAIDE command, by selecting the Open Visual Basic Editor option in the Open VBA Project dialog box, by clicking the Visual Basic Editor button in the VBA Manager dialog box, or by clicking the Edit button in the Macros dialog box.
The IDE is where you find yourself spending the majority of time coding, testing, and debugging any procedures and UserForms that are added to a project. A VBA project is a group of modules that is organized into a specific file structure. These modules include UserForms, class, and standard modules. The projects created in the IDE work only in AutoCAD and many of the AutoCADbased verticals such as Autodesk Architectural Desktop. So a project created for AutoCAD can’t be used in an application such as MS Word or MS Excel, although they are also host applications for VBA projects.
To load the Visual Basic Editor, choose Tools Macro Visual Basic Editor.
Exploring the IDE
The Visual Basic Editor is broken up into many different panes and areas; each of the panes and areas provides a set of functions that makes many of the programming tasks much easier. By default, three different panes and areas are visible at once. The four that you will most often use and should get comfortable with are
Project Explorer
Properties Window
Code and UserForm Windows
Object Browser
Project Explorer
The Project Explorer (see Figure 4-6) is used to access the properties and different modules that make up a VBA project. It provides an organizational view of the project to allow for opening and viewing the contents of a module in its proper editing window within the Visual Basic Editor.
1.From the View menu in the Visual Basic Editor, choose Project Explorer.
Choosing the option from the menu bar displays the corresponding pane.
2.Click the small button with an X located in the upper-right corner of the pane to close it.
Code and UserForm windows
Code and UserForm windows (see Figure 4-8) are the main editing areas found in the Visual Basic Editor. These windows are used for editing and designing UserForms and are where code is entered. Usually these windows take up the largest amount of room in the editor because they are where most of the work takes place. The Visual Basic Editor is a context-sensitive environment and controls the visibility of some utilities based on whether a Code or UserForm window is active. One example of this is the Toolbox, which is visible only when a UserForm window is active.
Figure 4-8:
Code and UserForm windows in the Visual Basic Editor, along with the Toolbox.
Object Browser
The Object Browser (see Figure 4-9) is a window into the world of VBA programming. It allows you to perform a search on the available objects that you have access to. These objects then can be viewed in the browser to see the different properties, methods, events, classes, and constants that they might contain.
730 Parts of a VBA Project
4.Select an attribute from the Members Of list box.
The Members Of list box displays the specific constant, properties, methods, events, and objects that are related to the class selected from the Classes list box. Notice that information is displayed at the bottom of the Object Browser about the selected item. This information might be the syntax for a method or an indicator that a property is read-only.
5.If you want additional information on the selected item from the Members Of list box or the Classes list box, you can press F1 on the keyboard. This launches the AutoCAD ActiveX and VBA Reference Online Help file (see Figure 4-10).
Figure 4-10:
AutoCAD
ActiveX and
VBA
Reference.
Parts of a VBA Project
A VBA project can be as simple as one class module called ThisDrawing and three lines of code, or as complex as a project that might include UserForms and both standard and class modules. The solution that is being developed determines the type of modules that will be a part of the final project. The following sections outline what the different modules in a project are and what is contained in each module.
Parts of a VBA Project 731
Standard code module
Standard code modules are used to store procedures, data types, variables, and functions that are accessed by other modules in the project. Code modules should be used to hold reusable code that can be used in future projects or called from multiple places in a project. This concept is called modular programming. It helps to avoid redundant code and also gives you the ability to reuse the code in other, similar projects.
Class code module
Class code modules are used to store procedures and properties that define a user-defined class. A user-defined class allows you to create your own object for storing information or manipulating other objects, similar to the way AutoCAD uses objects. All AutoCAD VBA projects contain a special class module called ThisDrawing. The ThisDrawing class represents the object that defines the current drawing in the AutoCAD session. Although you can create your own classes with VBA, you don’t often need to do it.
Procedures (subroutine or function)
A procedure is a block, or group, of code that is given a name so it can be referenced easily in a project. The two different types of procedures are sub and function. A sub, or subroutine, is a type of procedure that does not return a result; a function returns a single result after the procedure has completed all tasks and computations. Procedures to manipulate objects or return information about an object can be found throughout the AutoCAD Object Library. Some examples of procedures are
Function Add2WholeNumbers (Num1 as Integer, Num2 as Integer)
As Integer
Add2WholeNumbers = Num1 + Num2
End Function
Sub EchoValue (Value as Variant)
MsgBox Cstr(Value)
End Function
Here’s another example of using the previous two custom procedures Add2WholeNumbers and EchoValue:
EchoValue Add2WholeNumbers(1, 3)
Book X
Chapter 4
Basic VisualAutoCAD
for
This snippet displays a message box with the number 4 in it.