A common question asked by new VBA developers is how to automatically start a macro when you start MicroStation. There are several steps involved in this process.
- Set your VBA project to autoload when MicroStation starts. This can be done by either adding the name of your VBA project to the MS_VBAAUTOLOADPROJECTS workspace variable, or by simply checking the Auto-Load column in the VBA Project Manager dialog.
- In your code module you must create a sub named OnProjectLoad. This sub will be executed when your macro is loaded by MicroStation.
- The VBA is loaded prior to the MicroStation Manager dialog being displayed, or any file being opened. This means you must add in an event handler to monitor for file open and close events so that the VBA will know when MicroStation has opened a file.
- After the VBA knows a file is open you can call other Sub and/or Function routines to operate on the file.
The example code modules below show the required subs to automatically run a VBA macro.
Dim myOpen As clsFileOpenClose
' Autorun macro code goes here
MsgBox "Your macro is loaded – Welcome to MicroStation!"
Set myOpen = New clsFileOpenClose
Class Module Code - clsFileOpenClose
Dim WithEvents objApp As Application
Private isFirstFile As Boolean
Private Sub Class_Initialize()
Set objApp = Application
isFirstFile = True
Private Sub objApp_OnDesignFileClosed(ByVal FileName As String)
MsgBox "Closing file " & FileName, vbInformation
Private Sub objApp_OnDesignFileOpened(ByVal FileName As String)
' Do something different if this is the first file
If isFirstFile Then
MsgBox "Your first file is open: " & FileName
& vbCrLf & "Have a great CAD experience!", vbInformation
isFirstFile = False
MsgBox "Opening file " & FileName, vbInformation
' Call your automatic processing sub/function code from here