About Enabling Application Level Events (VBA/ActiveX)

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.

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.20.1")
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