Enable Application Level Events

AutoCAD ActiveX

 
Enable Application Level Events
 
 
 

Before you can use application level events you must create a new class module and declare an object of type AcadApplication 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.

To create a new class and declare an Application 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 App As AcadApplication

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 Application object. You can do this with the following code from any module.

To connect the declared object to the Application 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:
    Sub InitializeEvents()
    Set X.App = ThisDrawing.Application
    End Sub
  3. In the code for your main module, add a call to the InitializeEvents subroutine:
    Call InitializeEvents

Once the InitializeEvents procedure has been run, the App object in the class module points to the Application object specified, and any event procedures in the class module will run when the events occur.

Prompt to continue when a drawing is dropped into AutoCAD

This example intercepts the load process when a file has been dragged and dropped into AutoCAD. A message box containing the name of the file that was dropped and Yes/No/Continue buttons that allow the user to decide if the file should continue to be loaded or displayed. If the user chooses to cancel out of the operation, that decision is returned through the Cancel parameter of the BeginFileDrop event and the file is not loaded.

Public WithEvents ACADApp As AcadApplication
      
Sub Example_AcadApplication_Events()
 ' This example intializes the public variable (ACADApp)
 ' which will be used to intercept AcadApplication Events
 '
 ' Run this procedure FIRST!
      
 ' We could get the application from the ThisDocument
 ' object, but that would require having a drawing open,
 ' so we grab it from the system.
 Set ACADApp = GetObject(, "AutoCAD.Application.17")
End Sub
      
Private Sub ACADApp_BeginFileDrop _
 (ByVal FileName As String, Cancel As Boolean)
 ' This example intercepts an Application BeginFileDrop event.
 '
 ' This event is triggered when a drawing file is dragged
 ' into AutoCAD.
 '
 ' To trigger this example event:
 '     1) Make sure to run the example that initializes
 '     the public variable (named ACADApp) linked to this event.
 '
 '     2) Drag an AutoCAD drawing file into the AutoCAD
 '        application from either the Windows Desktop
 '        or Windows Explorer
      
 ' Use the "Cancel" variable to stop the loading of the
 ' dragged file, and the "FileName" variable to  notify
 ' the user which file is about to be dragged in.
      
 If MsgBox("AutoCAD is about to load " & FileName & vbCrLf _
 & "Do you want to continue loading this file?", _
 vbYesNoCancel + vbQuestion) <> vbYes Then
 Cancel = True
 End If
End Sub