Automatic execution when opening or closing drawings


       
  Applies To     
       
  Product(s): MicroStation  
  Version(s): V8i, CE, MS2024  
  Environment:  N/A  
  Area:  Programming  
  Subarea:  VBA  
  Original Author: Tristan Anderson, Bentley Technical Support Group  
       

 

We are often faced with the task of having to carry out actions during the opening or closing of drawings. These actions can include making adjustments or launching control routines that check and correct drawings.


Here is a small example created largely from the MicroStation VBA Help that is modified and compiled.

In the previous article explaining automatically loading VBA routines, it has already been noted that it is possible to automatically execute something, but this happens before the drawing file is opened.

Therefore, this example can be seen more as an extension. Because there is an event handler installed in this example, after running the VBA routine, every time you open or close a drawing, an event is intercepted. This makes it easy to implement something to run later when these events occur.

These events are indicated by a message box in this example, but this can be changed accordingly for your own use.
A module with the subroutine OnProjectLoad might look something like this:

 

Option Explicit
Private m_DGNOpenClose As clsDGNOpenClose

Public Sub OnProjectLoad()
    If m_DGNOpenClose Is Nothing Then
        Set m_DGNOpenClose = New clsDGNOpenClose
    End If
End Sub

Public Sub OnProjectUnload()
    Set m_DGNOpenClose = Nothing
End Sub

 

To load this VBA routine when you start MicroStation, it must be listed under the MS_VBAAUTOLOADPROJECTS, and the clsDGNOpenClose module must also be loaded.


Here is a class module clsDGNOpenClose that must be created separately in the defined class modules:

 

Option Explicit
Private WithEvents m_OpenCloseHooks As MicroStationDGN.Application

Private Sub Class_Initialize()
    Set m_OpenCloseHooks = MicroStationDGN.Application
End Sub

Private Sub Class_Terminate()
  Set m_OpenCloseHooks = Nothing
End Sub

Private Sub m_OpenCloseHooks_OnDesignFileOpened(ByVal DesignFileName As String)
    MsgBox ActiveDesignFile.FullName + " opened"
End Sub

Private Sub m_OpenCloseHooks_OnDesignFileClosed(ByVal DesignFileName As String)
    MsgBox ActiveDesignFile.FullName + " closed"
End Sub

 

In this example, two events OnDesignFileOpened and OnDesignFileClosed are intercepted when opening and closing a drawing. They both open a message box indicating whether the file has been opened or closed.


For simplicity, this example is stored as a complete VBA project and can be downloaded here:

VBA routine to install and event handler


Other language sources

 
Deutsch