About Enabling the Object Level Event (VBA/ActiveX)

Before you can use object level events you must create a new class module and declare an object of type AcadObject with events.

For example, assume that a new class module is created and called EventClassModule. The new class module contains the declaration of the application with the VBA keyword WithEvents.

Display the area of a closed polyline whenever the polyline is updated

This example creates a lightweight polyline with events. The event handler for the polyline then displays the new area whenever the polyline is changed. To trigger the event, simply change the size of the polyline in AutoCAD. Remember that you must run the CreatePLineWithEvents subroutine before the event handler is activated.

Public WithEvents PLine As AcadLWPolyline

Sub CreatePLineWithEvents()
 ' This example creates a light weight polyline
 Dim points(0 To 9) As Double
 points(0) = 1: points(1) = 1
 points(2) = 1: points(3) = 2
 points(4) = 2: points(5) = 2
 points(6) = 3: points(7) = 3
 points(8) = 3: points(9) = 2
 Set PLine = ThisDrawing.ModelSpace. _
 AddLightWeightPolyline(points)
 PLine.Closed = True
 ThisDrawing.Application.ZoomAll
End Sub

Private Sub PLine_Modified _
 (ByVal pObject As AutoCAD.IAcadObject)
 ' This event is triggered when the polyline is resized.
 ' If the polyline is deleted the modified event is still
 ' triggered, so we use the error handler to avoid
 ' reading data from a deleted object.
 On Error GoTo ERRORHANDLER
 MsgBox "The area of " & pObject.ObjectName & " is: " _
 & pObject.Area
 Exit Sub

ERRORHANDLER:
 MsgBox Err.Description
End Sub