Enable Document Level Events in Environments Other Than VBA
 
 
 

Before you can use document level events in VB or another environment besides VBA, you must create a new class module and declare an object of type AcadDocument with events. For example, assume a new class module is created and called EventClassModule. The new class module contains the declaration of the application with the VBA keyword WithEvents.

To create a new class and declare a Document object with events

  1. In the VBA IDE, insert a class module. From the Insert menu, choose Class Module.
  2. Select the new class module in the Project window.
  3. Change the name of the class in the Properties window to EventClass-Module.
  4. Open the Code window for the class using F7, or by selecting the menu option View Code.
  5. In the Code window for the class, add the following line:
Public WithEvents Doc As AcadDocument

After the new object has been declared with events, it appears in the Object drop-down list box in the class module, and you can write event procedures for the new object in the class module. (When you select the new object in the Object box, the valid events for that object are listed in the Procedure drop-down list box.)

Before the procedures will run, however, you must connect the declared object in the class module with the Document object. You can do this with the following code from any module.

To connect the declared object to the Document object

  1. In the Code window for your main module, add the following line to the declarations section:
    Dim X As New EventClassModule
    
  2. In the same window, add the following subroutine:
    ub InitializeEvents()
    
     Set X.Doc = ThisDrawing
    
    End Sub
    
  3. In the code for your main module, add a call to the InitializeApp subroutine:
    Call InitializeEvents
    

    Once the InitializeEvents procedure has been run, the Doc object in the class module points to the Document object created, and any event procedures in the class module will run when the events occur.