Code Document Level Events in VBA
 
 
 

Document level events are automatically enabled when a VBA project is loaded. To write event handlers for document level events in VBA, you simply select AcadDocument from the Object drop-down list in the Code window. The available events for the document will appear in the Procedure drop-down list. Simply select the event you want to write a handler for and the handler skeleton is created automatically.

Note that event handlers created in this fashion apply to the current active drawing. To create event handlers for a specific drawing, first follow the steps listed in Enable Document Level Events in Environments Other Than VBA. This will allow you to enable a specific document for events.

The following example uses the event handler for the BeginShortcutMenuDefault event to add the “OpenDWG” menu item to the beginning of the shortcut menu. Then the event handler for the EndShortcutMenu event removes the additional menu item so that it is not saved permanently in the user's menu configuration.

Private Sub AcadDocument_BeginShortcutMenuDefault _
					(ShortcutMenu As AutoCAD.IAcadPopupMenu)
	On Error Resume Next
	' Add a menu item to the cursor menu
	Dim newMenuItem As AcadPopupMenuItem
	Dim openMacro As String
	openMacro = Chr(vbKeyEscape) + Chr(vbKeyEscape) + "_open "
	Set newMenuItem = ShortcutMenu.AddMenuItem _
									(0, Chr(Asc("&")) _
									 + "OpenDWG", openMacro)
End Sub


Private Sub AcadDocument_EndShortcutMenu _
				 (ShortcutMenu As AutoCAD.IAcadPopupMenu)
	On Error Resume Next
	ShortcutMenu.Item("OpenDWG").Delete
End Sub