Vbarun and the Macros Dialog

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.

Figure 1-23. The AutoCAD command prompt

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.

Figure 1-23. The AutoCAD command prompt

You can also execute a macro through the Macros dialog box, shown in Figure 1-24. To open this dialog box, type VBARUN at the AutoCAD command prompt. You can also choose Tools > Macro > Macros or press Alt+F8. For the beginner or end user, this is an excellent way to execute macros.

Figure 1-24. The Macros dialog box

Macros

Tasks that occur frequently or are of a complex nature are ideal candidates for a macro. Using a macro gives you a more consistent and convenient way to accomplish these tasks. Macros let you create this process once and then reuse it multiple times.

You can create macros in one of two places, in either the ThisDrawing module or in a standard module. In either case, always declare them as Public subprocedures and not functions that return a value and can't receive arguments. Despite these limitations, macros are a powerful tool for increasing AutoCAD end-user productivity.

An example of where you might want to use a macro is in initializing user preferences whenever a drawing is created or opened, as in the following code:

Public Sub Setup()

Application.Preferences.OpenSave.AutoSavelnterval = 15 End Sub

In this example, AutoCAD will save the user's work every 15 minutes. You might consider using a macro similar to this to set various system variables for each drawing that you work on or perhaps for when each AutoCAD session begins. The previous section explained how to make VBA routines execute automatically for the start of either an AutoCAD session or a drawing.

The Macros Dialog Box

Use the Macros dialog box to create, run, debug, edit, and delete macros. You can also control how a macro runs using several options. This section explains each button in this dialog box.

This button executes your macro. You can also execute your macro from within the VBAIDE by choosing Run > Run or by pressing F5.

Step Into

This button lets you step line by line through the code of the macro you select. It enters the VBAIDE in step (or debug) mode at the beginning of the selected macro. You can also start Step Into from within the VBAIDE by choosing Debug > Step Into or by pressing F8.

Edit

This button lets you edit your macro's code. It opens the VBAIDE in edit mode at the beginning of the selected macro. This is identical to typing VBAIDE at the AutoCAD command prompt and navigating to the appropriate project, module, and macro.

Create

To create a macro, enter a name in the Macros dialog box and press the Create button. This opens a dialog box that lets you choose where you want to create your macro. You can also specify the project in which to create the macro. If you do not specify a project name, the Select Project dialog box, shown in Figure 1-25, appears. Choose from the list of loaded projects.

Delete

To delete a macro from the project file, select the Delete button. If you accidentally delete a macro, retrieve it by immediately switching to the VBA editor, bring the module that contained your source code into focus, and either choose Edit > Undo or press Ctrl+Z.

Figure 1-25. The Select Project dialog box

Caution This command deletes the source code associated with the macro. Be careful!

VBA Manager

This button opens the VBA Manager dialog box, which lets you create, edit, load, unload, close, embed, and extract your VBA projects.

Note For more about this dialog box, see "Creating, Opening, and Saving Projects," earlier in this chapter.

Options

AutoCAD VBA provides three options for controlling how AutoCAD handles VBA projects by default. Table 1-1 explains these options.

Table 1-1. VBA Options in AutoCAD

Option_Meaning_Default Value_

Enable Auto Embedding Determines whether a VBA project will False or unchecked be embedded in the current drawing

Allow Break on Errors Determines whether VBA is instructed True or checked to enter Break mode when an error occurs in your application

Enable Macro Virus Protection Determines whether macro virus True or checked protection is enabled

Using the Text Editor

Fortunately, the VBA text editor is not just a plain old text editor. It has some pretty nice features that make your application-development efforts more efficient. This section tells you how to take advantage of their usefulness.

Auto List Members

When you type a valid object name in your code and then type a period (.), VBA drops down a list of properties and methods available for that object, as shown in Figure 1-26. Typing the first few letters of the property or method name moves you to that selection in the list. Pressing the Tab key completes the typing for you. You can also press the Complete Word button on the Edit toolbar. This option is also helpful when you aren't sure which properties are available for an object.

Visual Basic 6 and VB.NET programmers know this feature by its other name: IntelliSense.

Intellisense Visual Basic
Figure 1-26. An Auto List Members example

Tip If you disable the Auto List feature, you can still access it by pressing Ctrl+J.

Auto Quick Info

Auto Quick Info displays the syntax of statements, procedures, and functions that are early-bound (there is a reference set to the object library you're using). This feature is not available for late-bound objects (no reference set to the object library). When you type the name of a valid Visual Basic statement, procedure, or function, the IDE shows the syntax immediately below the current line, with the first argument in boldface, as shown in Figure 1-27. After entering the first argument value, the second argument appears in boldface, and so on.

Figure 1-27. An Auto Quick Info example

Tip If you disable the Auto Quick Info feature, you can still access it by pressing Ctrl+I.

You may have noticed that this feature also provides you with a function's return value, which can be very helpful when you use an unfamiliar function for the first time.

0 0

Responses

  • marcel
    How to make a button to open vba in autocad 2011?
    8 years ago
  • janina
    How to create and run macros in autocad VBA?
    8 years ago
  • daniela
    How come vbarun doesnt work on autocad 2011?
    6 years ago

Post a comment