Loading VBA Project Files (Creating VBA Macros) (AutoCAD VBA)

When you open a drawing file from the AutoCAD window, by default no VBA project file is loaded into memory. You’ll need to load your project file manually before you can edit or run your macros. This section shows you how to change that.

If you prefer that your macros be loaded automatically, there are two options that you can set in the AutoCAD application so that specific files are loaded in one of two ways:

•    Every time AutoCAD starts up.

•    Whenever the current AutoCAD application is opened.

Loading a Project Manually

Manually loading the project file containing your macro is the best way to load macros that you don’t need to run very often. The following steps explain how to do this:

1. From the AutoCAD window, choose Tools ^ Macro ^ Load Project. The Open VBA Project dialog box (see Figure 2.11) appears with a list of the project files contained in the current directory.

Selecting a project from the Open VBA Project dialog box


Figure 2.11 Selecting a project from the Open VBA Project dialog box

2. Ensure that the Open Visual Basic Editor check box (at the bottom of the dialog box) is marked, select the project containing the required macro, and click Open. If the AutoCAD message shown in Figure 2.12 appears, click Enable Macros to continue. The Code window for ThisDrawing appears, containing the DrawText macro.

This message box will always appear if its Always Ask Before Opening Projects With Macros check box is selected. If you don’t want this warning message again, then simply uncheck the box. You can make the warning message show up again by clicking the Options button in the Macros dialog box (Figure 2.8) and selecting the Enable Macro Virus Protection option.

 Message box giving information about macros

Figure 2.12 Message box giving information about macros

If you want the IDE to open so that you can edit the macro being loaded, make sure that the Open Visual Basic Editor check box is selected in the bottom-left corner of the Open VBA Project dialog box.

Loading a Project Each Time a Drawing Object Is Opened

If you need to run a specific macro just about every time you work with a particular AutoCAD drawing object, consider setting up AutoCAD so that it will automatically load this macro for you every time you open the drawing object. The following steps show you how this is achieved:

1.    In AutoCAD, choose Tools ^ Macro ^ VBA Manager. The VBA Manager dialog box is displayed.

2.    Select the project file to be loaded, and click the Embed button.

3.    Close the dialog box and return to the AutoCAD window.

Loading a Project When AutoCAD Starts Up

If a macro is required by most of your AutoCAD drawings, consider setting up AutoCAD so that the project file containing the macro is loaded every time AutoCAD is opened. To load a project file every time AutoCAD starts up, follow these steps:

1.    Choose Tools ^ Load Application from the AutoCAD window. The Load/Unload Applications dialog box is displayed.

2.    Click the Contents button in the Startup Suite frame. The Startup Suite dialog box (Figure 2.13) opens, listing all the project files that are automatically loaded when you start AutoCAD. (There were none on my PC.)

3.    In the Startup Suite dialog box, Click Add. This opens the Add File To Startup Suite dialog box shown in Figure 2.14.

4.    Select the project file containing the required macro and click Add. This returns you to the Startup Suite dialog box, which now contains the project file you’ve just added.

5.    Close the Startup Suite dialog box. This returns you to the Load/Unload Applications dialog box, which now contains the text “Project.dvb was added to the Startup suite” in the frame along the bottom.

Startup Suite dialog box

Figure 2.13 Startup Suite dialog box

Adding a file to the elements that run when AutoCAD starts up

Figure 2.14 Adding a file to the elements that run when AutoCAD starts up

6. Click Close to return to the AutoCAD window. Your macro is now available for running at any time and will always appear in the list displayed in the Macros dialog box.

Canceling the Loading of a Project at Startup

If a project file always loads when AutoCAD is opened and you decide this is no longer necessary, follow these steps to remove it from the list in the Startup Suite dialog box:

1.    In the AutoCAD window, choose Tools ^ Load Application. The Load/Unload Applications dialog box opens.

2.    Click the Contents button in the Startup Suite frame. The Startup Suite dialog box opens (Figure 2.14), listing all the project files that are automatically loaded when you start AutoCAD.

3.    Select the project file you want to remove and click Remove. The name of the project file is no longer listed.

4.    Click Close to return to the Load/Unload Applications dialog box.

5.    Click Close to return to AutoCAD.

You can now open AutoCAD without the macro being loaded. If you occasionally need to use the macro again, you can always load it manually or add it back into the Startup list.

Next post:

Previous post: