Using VBA in AutoCAD r14.01, 2000, 2000i, 2002, 2004, 2005, and...
2006
By: Frank Zander
At: Contract CADD Group
Website: http://www.contractcaddgroup.com/
Email: frank.zander@contractcaddgroup.com
Phone: (604) 591-1140
This article explores the AutoCAD r14.01 version of VBA. The
topics covered include; what is VBA, why one would want to use VBA in AutoCAD,
how to setup/access AutoCAD r14.01's VBA, how an object (line, circle, etc) is
described by AutoCAD's VBA object model, how Forms and controls on Forms act as
VBA objects. For the curious, I have created a small visibility program
using VBA with AutoCAD 14.01. This article was written for the non-programmer
and hopefully the instructions will entice the non-programmer to try a hand at
creating a simple visibility VBA program. For those who do not want
to type in the code used in this article, the visibility program is
available for downloading at: http://www.contractcaddgroup.com/download/hideshow.zip
What is VBA anyway?
Visual Basic for Applications (VBA) allows Visual Basic (VB) programmers to
work in a familiar Visual Basic environment. Those not familiar with the Visual
Basic environment will find that the interface is easy to use and straight
forward.
Why would one use VBA in AutoCAD when LISP is so well supported in AutoCAD?
Three reasons:
- Speed
To quote Autodesk -- "VBA is fast. VBA is hosted by AutoCAD and
doesn't have the associated overhead of calling out to a separate process. In
internal benchmarks, VBA is significantly faster than AutoLISP® or Visual
Basic running as a separate application. The execution speed is very close to
a compiled C++ ObjectARX DLL-based extension." Programs created with
Visual Basic for Applications execute faster than programs created outside of
an application. For example, a VBA program will run faster than the same code
setup as a standalone VB executable (exe) program.
- Ease of use.
Visual Basic/VBA interface is easy to learn and use. VBA-enabled programs
(e.g. Word 97, Office 97 Excel 97, AutoCAD 14.01, Visio Technical,
IntelliCAD, etc) allow the user to learn and program in the same Visual
Basic environment.
- Universality
VBA has become broadly accepted as the Windows-based customization tool
of choice. The VBA interface resembles Visual Basic 5 (VB5). AutoCAD users
only need to learn one programming environment (VBA) to be able to program
with VB in all VBA enabled programs. The VBA editor included AutoCAD uses
the same programming environment used in Office 97 applications. LISP only
works in a handful of CADD programs (e.g. AutoCAD, IntelliCAD, Cadkey),
Lisp is clunky, as well as hard to learn.
The VBA interface:

With Visual Basic, the programmer creates a user interface by adding controls
from the Toolbox to a Form. To create a push button only requires a selection of
the button control in the Toolbox and drawing/placing the control on a Form. It
is very easy to create a Form that looks the way it will be used as a program.
Buttons to push, Text boxes for entry, Option lists, etc, are a breeze to
create, size, and change. The ability to rapidly create a prototype interface
for a new program is stunning to say the least. Also, the resulting Form can be
saved and imported into any Windows VBA-enabled program.
How to setup AutoCAD for VBA.
To use VBA for AutoCAD requires AutoCAD r14.01. If you do not have the 14.01
version see your local Autodesk dealer for details on how to obtain the r14.01
update. When installing or updating to AutoCAD r14.01 also select the install
VBA option (default on Typical and Custom installation settings).
To start the VBA editor:
To start the VBA editor for a new project inside of AutoCAD do the
following:
- From the pull down menu select: Tools | Macro | Visual
Basic Editor
Or at the command prompt type: VBAide
Or for AutoCAD 2000: ALT + F11
To load an existing VBA project into AutoCAD do the following:
- From the pull down menu select: Tools | Macro | Load
Project…

Describing Objects in VBA.
VBA uses Objects to work within documents. VBA describes everything within an
AutoCAD drawing (lines, circles, arcs, text, etc) as an Object. Every Object in
a drawing then has properties. A line within the current drawing in model space
has a color property that would be described as Thisdrawing.ModelSpace.lineobject.color
. A full listing and graphical representation of the AutoCAD Object model as
well as Methods, Properties, Constants, VBA events and example code — can be
accessed in AutoCAD from the online help (press F1 help) as shown below.

Every object (button, textbox, combobox, etc) on a Form becomes a
sub-object of the Form. For example the Off button text on the Visibility form
could be described as frmVisibility.cmdHideObjects.Caption = "Off"

Creating a small "Visibility" program using VBA inside of AutoCAD.
In planning this article, I wanted to demonstrate a simple program that would
be useful to all AutoCAD users and not be specific to one discipline (e.g.
Mechanical, Structural, or Architectural, etc.). I also wanted to create a very
simple program that would be easy for the non-programmer to create and
understand. Hopefully I have achieved this with the Visibility program. The
Visibility program allows the user to select any drawing entity in AutoCAD and
make the object(s) selected non-visible and later make the non-visible objects
Visible. Say you are working on a very-crowed drawing and only need a few of the
existing lines (circles, blocks etc.) to work on. Traditionally one would turn
off layers and only work with the remaining layers displayed. But what if you
need to work on several layers and need some of the drawing entities on those
layers not to show? Hmm… One could erase the unwanted drawing entities, draw
the required information and then OOPS the erased entities back into the
drawing. However, what happens if you forget to OOPS the erased entities back
into the drawing before saving and exiting the drawing? Or what happens if you
erase more entities after your original erase? Well we all know those answers…
You loose your erased objects. But what if I told you that one of the modifiable
object properties of any AutoCAD object is its Visibility? Yes, you can make
objects non-visible! And then at any time make the non-visible objects visible!
So irrespective of drawing layer settings, anything you can select with the Visibility
program, will make the individual selected entities (objects) non-visible. Think
of it as having an OBJECT freeze / thaw command. Other uses of the Visibility
program could include embedding non-displaying copyright information, or
creating non-displaying reference geometry for future reference.
To make the Visibility project:
Creating the Visibility Form:
- At the AutoCAD command prompt type: VBAide
- From the VBAide pull down menus select Insert | UserForm.
- Press F4 to go to the properties-UserForm1
window.
- Change the (name) field from UserForm1 to frmVisibility
- Change the Caption field from UserForm1 to Visibility
- Press F5 to check out your new form and how it will display in
AutoCAD.
- Press the X in the top right hand corner to close the form.
Creating the Off Button on the Visibility Form:
- From the VBAide pull down menus select View | Toolbox
- Press the command button in the Toolbox controls.

- Draw the command button (make a window) on the visibility form
(frmVisibility)
- Press F4 to go to the properties-CommandButton1 window.
- Change the (name) field from CommandButton1 to cmdHideObjects
- Change the Caption field from cmdHideObjects to Off
- Press F5 to check out your form and new Off button to see
how it will display in AutoCAD.
- Press the X in the top right hand corner to close the form.
Saving your project:
- From the VBAide pull down menus select File | Save
- Save this project as visibility.dvb
- Note if you save this project as PROJECT.DVB in your AutoCAD directory, it
will automatically load for each AutoCAD session.
Creating the On button for the Visibility Form:
- Press the command button in the Toolbox controls.
- Draw the command button (make a window) on the visibility form
(frmVisibility)
- Press F4 to go to the properties-CommandButton1 window
- Change the (name) field from CommandButton1 to cmdShow
- Change the Caption field from cmdShow to On
- Press F5 to check out your form and new On button to see how it
will display in AutoCAD.
- Press the X in the top right hand corner to close the form.
Creating the Exit button for the Visibility Form:
- Press the command button in the Toolbox controls.
- Draw the command button (make a window) on the visibility form
(frmVisibility)
- Press F4 to go to the properties-CommandButton1 window
- Change the (name) field from CommandButton1 to cmdExit
- Change the Caption field from cmdExit to Exit
- Press F5 to check out your form and new Exit button to see how it
will display in AutoCAD.
- Press the X in the top right hand corner to close the form.
Creating the click event on the Exit button:
- So far the command button is in location and shows "Exit", but
does nothing. Why? You have not assigned an event to the command button.
What is an event? An event is what happens when a user changes something or
interacts with a program. The event you want to monitor is when the user clicks
the Exit button. In the VBA ide window Double click the
Exit command button quickly. Notice that VBA Form Object view window changes
to a View Code window. Also notice that you are presented with the code
following:
Private Sub cmdExit_Click()
End Sub
In between Private Sub and
End Sub is where you write code… Add the code Unload Me between
Private Sub cmdExit_Click() and
End Sub as shown in the example code in The Code for the Visibility
program. Now when the Visibility form is displayed in AutoCAD, the Exit
button will unload the Visibility Form. Add comments to you program to make
it easier to understand. By placing a ' in front of the comments, the VBA
editor will ignore anything with a ' in front of it
and automatically turn it green…
To create the click events for the Off button see Private
Sub cmdHideObjects_Click() as shown below in The Code for the
Visibility program.
To create the click events for the On button see Private
Sub cmdShow_Click() as shown below in The Code for the Visibility
program.
The Code for the Visibility program.
View here | Download
here
|
|
|