AutoCAD VBA Programming: Tools and Techniques

VBA runs inside AutoCAD; when the AutoCAD drawing editor is active, there is always a current drawing. The current drawing (or document) is defined as an object with the name ThisDrawing in the VBA environment. ThisDrawing is essentially the trunk of the object tree with the application object being the root. Programs can control opening, saving, and other file-based operations of the AutoCAD system using the associated methods. This is not the case when writing applications in Visual BASIC (VB) and using the ActiveX interface options.
When programming in VB (not VBA), which is running outside of AutoCAD, the program must first establish a link to the AutoCAD application, and from there to the active document. In VB, you must first reference the AutoCAD object model before your program can make use of the AutoCAD objects. Under the Projects menu you can find the References option. Inside the References command is where the reference to AutoCAD's object model is defined.
The following code example demonstrates how VB can interface to the drawing by setting a global variable for the document object (acadDOC) after the AutoCAD object library has been referenced into the project. This variable has been dimensioned as an object in a global section of the program. After you have made this assignment, you can explore the object tree by referencing that object variable in the same manner that ThisDrawing would be used in a VBA program.
On Error Resume Next...