Creating a simple macro

AutoCAD Map 3D ActiveX

Creating a simple macro

You create and run a VBA macro from either AutoCAD Map or the Visual Basic Editor.

To create and run a macro from AutoCAD Map

1 Choose Tools > Macro > Macros.

2 Type the name of the macro, for example Hello.

3 Click Create.

4 Select a project or drawing.

VBA displays a code window and inserts the following statements in it: Sub Hello, and End Sub. By default, the Option Explicit statement is displayed above Sub Hello , to force you to declare variables in a module. If option to include this statement is turned off, you do not see Option Explicit.

Between the Sub Hello and End Sub statements, type the following code:

MsgBox "Hello"

6 Choose Run > Run Sub/User Form to run the code. To step through the code, put the cursor between the Sub and End Sub statements, and press F8. To run the code, choose Run > Run Sub.

The Hello message appears in AutoCAD Map.

To create and run a macro from the Visual Basic Editor

Double-click ThisDrawing in the Project Explorer.

2 Choose Insert > Procedure and type the name for the procedure. For example, type Hello.

VBA inserts the following statements: Sub Hello, End Sub, and Option Explicit, which forces you to declare variables in a module if the option to include this statement is turned on.

Between the Sub Hello and End Sub statements, type the following code:

MsgBox "Hello"

4 To step through the code, put the cursor between the Sub and End Sub statements, and press F8. To run the code, choose Run > Run Sub.

The Hello message appears in AutoCAD Map.