Load and Run VBA Macros Using Straight VBA

15 Sep, 2004 By: Mike Tuersley

Contrary to popular belief, the process doesn’t require AutoLISP

Welcome back! This month we're going to look at how to automatically load and run VBA macros using straight VBA. The idea for this article came when I noticed the same question repeated over and over again on the newsgroups and realized that most VBA programmers have no idea how to do this. Most assume it requires AutoLISP.

The reality is that the process requires AutoLISP or Visual LISP only if the end user needs a command-line shortcut to a macro. Even then, you don't necessarily need a LISP file, as I'll demonstrate. What the process does require are two files we need to create ACAD.DVB and ACAD.RX. Let's start with the ACAD.DVB file.

Figure 1. Project window in AutoCAD's VBA IDE.
First, fire up AutoCAD. Then go into the VBA Manager and create a new file. While in the VBA Manager dialog box, save the new file as ACAD.DVB. Now, open the VBA IDE and start programming.

In the IDE, no modules or forms should exist. The code we're going to write will reside within the ThisDrawing module in AutoCAD Objects (figure 1).

Inside ThisDrawing, a subroutine named ACADStartup must exist. Whatever you place within this subroutine runs once the next time you launch AutoCAD, not every time you open a drawing. Here's an example:

Sub ACADStartup()
  Call AcadApplication.RunMacro_
End Sub

This loads the sample DVB file that adds a toolbar specific to VBA found in the AUTOCAD\SAMPLE\VBA\VBAIDEMENU directory, assuming AutoCAD was installed using the Full or Custom option.

The next step is to open Windows' Notepad or a similar text editor and create a new file. In it, enter this line of text:

ACVBA.ARX, if AutoCAD 2004+; ACADVBA.ARX if <=AutoCAD 2002

Save the file as ACAD.RX, making sure you set the file Save As Type to ALL FILES. The ACAD.RX file is just an ASCII text file that tells AutoCAD which ARX files to automatically load at startup. Placing either ACVBA or ACADVBA in it tells AutoCAD to initialize the VBA system. This in turn causes AutoCAD to look for an ACAD.DVB file. AutoCAD uses its Search path settings to find the first ACAD.RX and first ACAD.DVB files to load. This means you could have a set of both files for every project directory if you need to load different files on a per-project basis.

That's it! It's very straight forward.

Kick It Up a Notch
Suppose you want a macro to run every time you open a drawing. The startup doesn't fulfill this requirement, as stated previously, because it loads only once -- during startup. Hence the name. What we can do is add some event programming. Before we go any further, though, please proceed into events with caution. Event programming can have undesirable results if you're not careful!

Figure 2. The VBAIDE Object and Procedure/Event boxes.

Events are actions an object can perform that you can monitor. AutoCAD has two main types of events: document-level and application-level. We'll look at the document-level events for this next tip.

Because opening a drawing is an action, you can monitor it. To add an event handler to the ACAD.DVB file, select the AcadDocument listing in the Object box. Then select the appropriate event in the Procedures/Events box (figure 2.)

Select EndCommand and you see the following skeleton appear in the VBA Editor: Private Sub AcadDocument_EndCommand(ByVal CommandName As String) End Sub

This states that at the end of any command, an event starts and passes the name of the command that is ending. You just need to act on the appropriate command name as follows:

Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
  If UCase(CommandName) Like "OPEN" _
    Or UCase(CommandName) Like "NEW" _
      Or UCase(CommndName) Like "QNEW" Then
    MsgBox "Opening or starting a new drawing", _
      vbOKOnly, "Event Test"
  End If
End Sub

Now, whatever you specify executes each time you open or create a drawing. Remember that when you first start AutoCAD, both the startup and event codes execute.

Kick it Up Another Notch
The next kicker is adding a command-line definition directly from our DVB file. Instead of having a LISP file do it, you can combine a LISP statement and the SendCommand to accommodate this option. Anyone familiar with my posts on the newsgroups should know I don't advocate using the SendCommand or mixing programming languages. However, in this case, there's no better solution available unless you do it in ARX or NET. So, here is the LISP statement you would normally place within a LISP file or attach to a toolbar button:

 (Defun C:VBAT ()
  (vl-vbarun "c:/{your install path}/ Custom_menu.dvb!CreateVBAToolBar"))

This lets you type in VBAT at the command line or attach it to a toolbar button to have the CUSTOM_MENU.DVB file load and execute the CreateVBAToolBar macro. Notice that the Visual LISP command VL-VBARUN not only runs the macro but also loads it if it's not already loaded. Add this LISP command to the ACADStartup routine like this:

ThisDrawing.SendCommand "(vl-load-com)" & vbCr

Dim sLispDef As String

sLispDef = "(Defun C:VBAT ()(vl-vbarun " & Chr(34) & _ 
".\Sample\VBA\VBAIDEmenu\Custom_menu.dvb!CreateVBAToolBar" _
& Chr(34) & "))"

ThisDrawing.SendCommand sLispDef & vbCr

Before this can work, make sure that Visual LISP has been started by passing the VL-LOAD-COM command call. Within the actual LISP command definition, we've added two calls to the ASCII Character function, CHR(), to add the quotation marks (") required around the DVB's file name.

I encourage you to explore using multiple RX files and to venture into more event programming. Be sure to test any event operations on noncritical drawings to avoid those unexpected results mentioned earlier.

About the Author: Mike Tuersley

AutoCAD Tips!

Lynn Allen

In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!

Follow Lynn on TwitterFollow Lynn on Twitter

Are you responsible for any CAD management duties (conducting training, implementing software, establishing standards, etc.)?
Yes: I am a full-time CAD manager
Yes: CAD management is part of my job description
Yes: CAD management is not officially part of my job, but there's no one else to do it
Submit Vote

Download Cadalyst, Fall 2015

Download Cadalyst Magazine Special Edition