About Referencing Objects in the Object Hierarchy (VBA/ActiveX)

You can reference objects directly or through a user-defined variable. To reference the objects directly, include the object in the calling hierarchy.

For example, the following statement adds a line in model space. Notice that the hierarchy starts with ThisDrawing, goes to the ModelSpace object, and then calls the AddLine method:

Dim startPoint(0 To 2) As Double, endPoint(0 To 2) As Double
Dim LineObj as AcadLine
startPoint(0) = 0: startPoint(1) = 0: startPoint(2) = 0
endPoint(0) = 30: endPoint(1) = 20: endPoint(2) = 0
Set LineObj = ThisDrawing.ModelSpace.AddLine(startPoint,endPoint)

To reference the objects through a user-defined variable, define the variable as the desired type, then set the variable to the appropriate object. For example, the following code defines a variable (moSpace) of type AcadModelSpace and sets the variable equal to the current model space:

Dim moSpace As AcadModelSpace
Set moSpace = ThisDrawing.ModelSpace

The following statement then adds a line to the model space using the user-defined variable:

Dim startPoint(0 To 2) As Double, endPoint(0 To 2) As Double
Dim LineObj as AcadLine
startPoint(0) = 0: startPoint(1) = 0: startPoint(2) = 0
endPoint(0) = 30: endPoint(1) = 20: endPoint(2) = 0
Set LineObj = moSpace.AddLine(startPoint,endPoint)

Retrieving the first entity in model space

The following example returns the first entity object in model space. Similar code can do the same for paper space entities. Note that all drawing objects can be defined as AcadEntity objects:

Sub Ch2_FindFirstEntity()
    ' This example returns the first entity in model space
    On Error Resume Next

    Dim entity As AcadEntity
    If ThisDrawing.ModelSpace.count <> 0 Then
        Set entity = ThisDrawing.ModelSpace.Item(0)
        MsgBox entity.ObjectName + _
 " is the first entity in model space."
    Else
        MsgBox "There are no objects in model space."
    End If
End Sub