Extending product function with VBA > System Architect events > Guidelines for adding macro items to menus programmatically > Using the events
  
Using the events
The following SA2001.Application events can assist macro code when deciding when to make menu visibility changes:
Event
Description
MainMenuUpdate
When the whole menu is affected
MethodMenuUpdate
When dictionary type menu items are updated
ToolsMenuUpdate
When tools type menu items are updated
ReportsMenuUpdate
When report type menu items are updated
App_ShutDown
When System Architect shuts down
Events are no longer required to keep user items in the menus. However, to remove items from a menu, the following sections of code provide the outline for such event handling. The Project is called MyProject.
MODULE: AutoExec
Main module which maintains the Microsoft VBA based event handler.
Dim EventHandler As EventCls
' Main Subroutine which starts the event handler
Sub Main()
Set EventHandler = New EventCls ' Create an event handler
' Perform any additional start up work here
' This is a good place to add you BMPs and create your PopUp menus
InitBMPs
InitPopUps
End Sub
Sub InitBMPs()
Dim x As Integer
x = Application.AssignBMPtoMacroItem("MyProject.MyModule.PRINTDIAGRAMS", "SAWORD.BMP")
End Sub
Sub InitPopUps()
Dim x As Integer
' NB: Once a popup is created, it remains until it is removed by 'RemovePopupMenu', and can be added/removed from menus at will.
' Create the popup menu with its bitmap
x = Application.CreatePopUpMenu("Sample Macros","SAWORD.BMP")
' Add the item to the popup
x = Application.InsertMacroItemInMenu("MyProject.MyModule.PRINTDIAGRAMS", "&Print a Diagram", "Sample Macros")
End Sub
CLASS: EventCls
Public WithEvents App As Application
' The application which will raise events
Private Sub Class_Initialize()
Set App = Application ' Connect the event handler to SA
End Sub
Private Sub Class_Terminate()
' No need to do anything
End Sub
Private Sub App_ReportsMenuUpdate()
' Reports menu has been redrawn
Dim x As Long
x = App.SetSeparatorBefore("&Report Generator...", "&Reports", True)
' Insert a menu item in the 'Reports' menu, before the 'Report Generator...' item, called 'Print all Diagrams' which calls PrintDiagrams2
x = App.InsertMacroItemInMenu("MyProject.MyModule.PRINTDIAGRAMS2", "Print all &Diagrams", "&Reports", "&Report Generator...")
' Insert our popup menu 'Sample Macros' in the 'Reports' menu, before the 'Print all Diagrams' item.
' NB: You should not keep destroying and creating popups here, unless you have a good reason - do this once at initialization.
x = App.InsertPopupMenuItemInMenu("Sample Macros", "&Reports", "Print all &Diagrams")
End Sub
MODULE: MyModule
Public Sub PrintDiagrams()
' User Code to run
MsgBox "Print Diagrams"
End Sub
Public Sub PrintDiagrams2()
' User Code to run
MsgBox "Print Diagrams2"
End Sub
See also
Guidelines for adding macro items to menus programmatically