Overview of Objects, Properties, Methods, and Events (VBA Programming Concepts) (AutoCAD VBA) Part 1

VBA is an object-oriented environment, containing many different classes of objects. Each class has its own set of properties to define the appearance of the objects, and methods to manipulate the objects that are instances of that class.

Some classes, such as Toolbox controls, also have their own set of event procedures that are coded with the application’s response to user events. Don’t let the substantial number of these objects, properties, methods, and events overwhelm you—a little goes a long way. You actually only need to know about a few of them to get started.

All about Objects

Everything relating to an object is defined in its class. There are two main groups of AutoCAD objects:

Drawing Objects Drawing objects represent anything that has been drawn in the Model Space tab of the AutoCAD window (for another look at this window.Even when you draw a simple line, AutoCAD creates an object to store all the details needed to regenerate it.

VBA (UserForm) Objects UserForm objects represent anything that’s included in the graphical user interface (GUI). So each time you drag and drop a control from the Toolbox, an object is created that is an instance of that class of control.

Classes of Objects

In the object-oriented paradigm, each type of object and all its properties, methods, and events are encapsulated in a single class that has the same name as the type of object it contains. Classes can be thought of as templates for creating as many objects as you need. A class contains the code required to create an instance of that class. For example, when a new object is created, its Name property is assigned the name of the class it is based on, followed by a sequential number to distinguish the new object from any other instances of the same class.


Objects are said to be instances of classes, and their properties, methods, and events are said to be members of that class.

Collections of Objects

Related objects are grouped together into collections. Collections of objects provide the same benefits as arrays, in that items in the collection can all be referenced using a single name and an index value. A collection differs from an array, however, in that it can contain different types of items. For example, AutoCAD has a ModelSpace collection containing all the objects associated with the Model Space tab. VBA has a Forms collection that contains all the UserForm objects associated with the GUI, UserForms contain objects such as text boxes and command buttons that the user interacts with.

AutoCAD maintains several collections of objects, including the ModelSpace, PaperSpace, Documents, Layers, LineTypes, TextStyles, and ViewPorts collections, as well as heaps more. When you insert a UserForm into a project, VBA creates a Controls collection, and any control objects you drag and drop from the Toolbox are automatically added to the Control collection as you place them on the UserForm.

As already mentioned, objects in a collection—called members—need not belong to the same class. You can reference members in several ways; one way is to use an exclamation mark character (!) to associate the object with the collection:

tmp757e-162_thumb

If you are an Excel user you will already be familiar with this notation.

An alternative referencing method is to use an index to identify the member’s position within the collection. The “position” is the order in which it was placed on the UserForm:

tmp757e-163_thumb

This statement sets the Text property of the first object placed on UserForml to the string "Text box 1". You can also replace the index number with the control’s name if known:

tmp757e-164_thumb

Both examples of using indices make use of the fact that the Item method is the default method of any collection and so can be implied. The following statement explicitly uses the Item method and has a numerical index value (this is the format used throughout this topic):

tmp757e-165_thumb

This statement is said to “fully qualify" the TextBox object. Because the Text property is the default property of a text box, and the Item method is the default method of a collection, and the Controls collection is the default collection of a UserForm, and UserForm1 is the active UserForm, the above statement can be entered in the Code window belonging to UserForml as

tmp757e-166_thumb

with the UserForm, the Controls collection, the Item method, and the Text property all being implied.

AutoCAD Drawing Objects

AutoCAD has its own set of drawing objects that help you add the various components needed to construct a drawing. Figure 4.5 shows the hierarchical structure of the AutoCAD object model.

Attaching the Add prefix to a drawing object’s name is a common way to create drawing objects. For example, the following statement creates a Circle object:

tmp757e-167_thumb

The Application Object

The AutoCAD Application object sits at the top of the model and represents the current session of the AutoCAD application. As you draw items, their details are stored in objects and added to this Application object. Because only one AutoCAD Application object is allowed per session, the application can be implied when accessing the objects it contains. So, the statement example from the preceding section can be rewritten as

tmp757e-168_thumb

The Documents Collection

The AutoCAD Documents collection, which is the next level down from the Application object, allows you to access any document that’s currently open in the AutoCAD application. You use the Item method with an index value to denote the Document object’s position in the collection.

The AutoCAD object model

Figure 4.5 The AutoCAD object model

AutoCAD provides two ways to access the active (or current) document: via the ActiveDocument property (of the Application object), or by using the keyword ThisDrawing. These two methods are illustrated in the following two statements, which are equivalent:

tmp757e-170_thumb

You will need to use either of the above statements if you are entering code into a User-Form’s Code window.

If you are using ThisDrawing’s Code window, because only one document can be active at any point in time, the documents themselves can also be implied. So you can rewrite either of the preceding statements as follows:

tmp757e-171_thumb

The Documents collection includes the ModelSpace collection, which comprises all the objects from the Model Space tab, and the Layouts collection, which contains a Layout object for each Layout tab in the open AutoCAD application. Because the Document object can access any of the collections associated with drawing objects, and all the objects contained in these collections can be accessed individually, you can, in effect, access any drawing object in the currently open AutoCAD application. For example, the following For loop uses the Item method with Count as the index to access all the objects one by one from the ModelSpace collection, and assigns the value of the constant acRed to their Color properties:

tmp757e-172_thumb

The Microsoft Forms Object Model

AutoCAD’s VBA is an object-oriented programming environment that contains a large collection of objects. Figure 4.6 shows the hierarchical structure of the Forms object model.

Microsoft Forms object model

Figure 4.6 Microsoft Forms object model

To view the Microsoft Forms object model:

1.    Choose Help ^ Microsoft Visual Basic Help to display the Visual Basic References dialog box.

2.    Select the Contents tab and click Microsoft Forms Reference to expand its list.

3.    Double-click the Microsoft Forms Object Model Overview item. The Microsoft Forms object model appears in the right pane as shown in Figure 4.6.

Objects in the Forms object model have sets of properties, events, and methods that help you develop applications with which users can interact.

The Microsoft Forms Collection

The Microsoft Forms collection sits at the top of the model and contains all the UserForm objects from the next level down. A UserForm object, which can be a window or a dialog box, is the main part of the GUI; it acts like a container for the controls the user handles. Each UserForm has properties that are assigned values, allowing it to be maximized, minimized, or closed in the same way as any standard window. Other properties can be assigned text strings and colors to define how they will appear on the screen.

The Controls Collection

Each UserForm object has a Controls collection that contains all the Control objects that have been dragged from the Toolbox and positioned on the UserForm. A Control object from the Controls collection can be identified by a number representing the order in which it was placed on the UserForm. For example, a Label control that was the first object placed on UserForm1 can be accessed as

tmp757e-174_thumb

This gives you the advantage of being able to access all the controls in a collection using a For loop, as follows:

tmp757e-175_thumb

The For statement initially accesses the first control from the Controls collection and assigns it to the CurrentControl variable. The next statement (CurrentControl .BackColor = vbBlue) sets its BackColor property to blue. At the Next statement, execution loops back to the beginning of the For statement, and the next control in the collection is assigned to the CurrentControl variable.The looping continues until all the controls in the collection have been set to blue.

The Label control that you used in the Metric Imperial Converter application can also be accessed using the following formats:

tmp757e-176_thumb

If you are accessing a control from code entered into the Code window that belongs to the same UserForm as the control, you can refer to the control by name and the UserForm portion can be implied, like this:

tmp757e-177_thumb

The format I’ve chosen for this topic is to refer to controls by name only. The exception is when the control belongs to a different UserForm, when the name of the UserForm also needs to be included.

Next post:

Previous post: