Introduction

This article describes a VBA project that assists with MicroStation configuration variables. The original question, published on the Be Communities MicroStation Forum, was "How do I find the top-most folder of the active DGN file path?"

There's no simple way to answer an apparently simple question, so we wrote a VBA project to provide an answer. The remainder of this article covers some of the detail of the VBA coding.

Solution

We wrote a VBA project to solve this problem. The project runs silently until the user opens a new DGN file. The project analyses the new DGN file path, and finds the top-most folder. It sets a configuration variable to the top folder path.

By default, the configuration variable is named ROOT_DIR. You can change that in the VBA code to a different name.

For example, suppose you open a DGN file C:\Projects\Project1\dgn\file1.dgn. The VBA project sets configuration variable ROOT_DIR = C:\Projects.

If you want just to use the project, you can skip the VBA stuff and jump straight to the download section.

MicroStation Events

Questions similar to this appear on the Be Communities MicroStation Programming Forum.

MicroStation VBA lets a programmer watch certain events. For example, when a user opens a new DGN file, the programmer can catch the Design File Opened event and perform some action. The action is application-specific and the programmer must decide what action to take in a given situation.

MicroStation VBA provides several event handlers. To learn more, start at the Overview of MicroStation Events chapter in MicroStation VBA help.

Q Questions about MicroStation VBA and Event Handlers …

A  To qualify the question, the MicroStation user wants to find the first folder in the active design file's path.

The table below provides a couple of examples. The first column is the path to the active design file. The second column is what the questioner expects to see as the result of analysis …

DGN Path Result
C:\Projects\dgn\file1.dgn C:\
\\ServerName\Projects\Project1\dgn\file2.dgn \\ServerName\Projects

Find Root Folder

A  To find the root folder of a path, we use some of the tools provided by Microsoft's Windows Scripting library.

The Scripting library provides several useful objects. For our purposes we're interested in the FileSystemObject and the Folder object. Here's a function that, given a path, extracts the top-level folder …

' ---------------------------------------------------------------------
'   GetTopFolder
'   Given a folder path, return the top-most folder.
'   This method uses Microsoft's Scripting Runtime library.
'   Returns:    String top folder
' ---------------------------------------------------------------------
Public Function GetTopFolder(ByVal path As String) As String
    GetTopFolder = vbNullString
    Dim oFileSystem                         As New Scripting.FileSystemObject
    Dim oFolder                             As Scripting.Folder
    Set oFolder = oFileSystem.GetFolder(path)
    Debug.Print "First folder " & SingleQuote(oFolder.path)
    Dim oParent                             As Scripting.Folder
    Set oParent = oFolder.ParentFolder
    Do Until oParent Is Nothing
        Debug.Print "Parent folder " & SingleQuote(oParent.path)
        GetTopFolder = oParent.path
        Set oParent = oParent.ParentFolder
    Loop
    Set oFileSystem = Nothing
End Function

Files and Folders

You can see more examples of Windows Scripting on the Files and Folders page.

Find DGN File Root Folder

A  The path to the active design file is always available in MicroStation configuration variable _DGNDIR. When a user opens a new file, _DGNDIR is updated automatically by MicroStation. Get the value of a configuration variable using VBA's ActiveWorkspace object …

Dim path As String
path = ActiveWorkspace.ConfigurationVariableValue ("variable-name")

Some configuration variables may specify multiple paths. MS_RFDIR, for example, may point to several folders where MicroStation should look for reference files.

_DGNDIR always contains a single folder where the active DGN file is located. The following VBA code returns the top folder of the path to the active DGN file …

Dim path As String
path = ActiveWorkspace.ConfigurationVariableValue ("_DGNDIR")

VBA Project Events

A  Write a class that has a member variable of type Application …

Dim WithEvents m_oObserver As Application

When the class initialises, set that variable …

Private Sub Class_Initialize()
    Set m_oObserver = Application
End Sub

Write a method in a normal VBA module — for example, your Main module — named OnProjectLoad. MicroStation always looks for a subroutine having that name and no arguments. If MicroStation finds that subroutine it runs it automatically. Put whatever code you want in that subroutine. In our case, we want to start the DgnFileEvents observer …

Public g_oDgnEventObserver                  As clsDgnEventObserver
...
Public Sub OnProjectLoad()
    Debug.Print "[DgnFileEvents]modMain.OnProjectLoad"
    Set g_oDgnEventObserver = New clsDgnEventObserver
End Sub

VBA Design File Events

A  Now your class methods m_oObserver_OnDesignFileClosed and m_oObserver_OnDesignFileOpened are active and watching for those events …

Private Sub m_oObserver_OnDesignFileOpened(ByVal designFileName As String)
    Debug.Print "Opened DGN file " & SingleQuote(designFileName)
End Sub
Private Sub m_oObserver_OnDesignFileClosed(ByVal designFileName As String)
    Debug.Print "DGN file " & SingleQuote(designFileName) & " has closed"
End Sub

Automate Path Analysis

Event OnDesignFileOpened is the perfect place, in this example, to evaluate the _DGNDIR configuration variable …

Private Sub m_oObserver_OnDesignFileClosed(ByVal designFileName As String)
    Debug.Print "DGN file " & SingleQuote(designFileName) & " has closed"
    AnalysePathSetCfgVar "_DGNDIR", "ROOT_DIR"
End Sub
MicroStation Configuration showing ROOT_DIR

Method AnalysePathSetCfgVar analyses the first argument, in this case "_DGNDIR". It finds the top-level folder and then sets the configuration variable named in the second argument to that value.

Download

How to Obtain DgnFileEvents

The complete code is provided in the DgnFileEvents MVBA project. It's supplied in a ZIP archive. Unpack the ZIP archive to your ..\Workspace\Standards\VBA folder, where MicroStation can find it.

MicroStation VBA Project Manager

If you want MicroStation to run it automatically, then add the project to the list of auto-loaded VBA projects. You'll find that in the VBA Project Manager dialog, which you open from MicroStation menu Utilities|Macros|Project Manager.