アプリケーション レベルのイベントを使用するには、まず新しいクラス モジュールを作成し、イベントを使用できる AcadApplication タイプのオブジェクトを宣言する必要があります。
たとえば、EventClassModule という名前のクラス モジュールを新しく作成するとします。新しいクラス モジュールには、WithEvents という VBA キーワードを持つアプリケーションの宣言が含まれます。
以下の例では、ファイルが AutoCAD にドラッグ アンド ドロップされると、ロード処理が割り込まれます。メッセージ ボックスが表示され、ここにはドロップされたファイル名とユーザがロードを継続するかどうかを決定するための[はい]/[いいえ]/[継続]ボタンが含まれます。ユーザがキャンセルして操作を中止すると、その決定は BeginFileDrop イベントの Cancel パラメータを介して返され、ファイルはロードされません。
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")
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