Questions similar to this appear on the Bentley Discussion Groups. These problems appeared in the VBA discussion group.

Q

Current DGN File

A There's an undocumented configuration variable _dgnfile. It's updated automatically each time you open a DGN file. It points to the active DGN file's location. So the answer to the question is simple: evaluate_dgnfile …

' ---------------------------------------------------------------------
'   ShowCurrentDgnFile
'   Shows how to find the current DGN file path
' ---------------------------------------------------------------------
Sub ShowCurrentDgnFile()
    Dim path                                As String
    Const CurrentFile                       As String = "$(_dgnfile)"
    path = ActiveWorkspace.ExpandConfigurationVariable(CurrentFile)
    Debug.Print "Variable '" & CurrentFile & "' evaluates to " & path
End Sub

Current DGN File Location

A There's an undocumented configuration variable _dgnfile. It's updated automatically each time you open a DGN file. It points to the active DGN file's location. So the answer to the question is simple: evaluate_dgnfile …

' ---------------------------------------------------------------------
'   ShowCurrentDgnFolder
'   Shows how to find the current DGN file folder
' ---------------------------------------------------------------------
Sub ShowCurrentDgnFolder()
    Dim path                                As String
    Const CurrentFolder                     As String = "$(_dgndir)"
    path = ActiveWorkspace.ExpandConfigurationVariable(CurrentFolder)
    Debug.Print "Variable '" & CurrentFolder & "' evaluates to " & path
End Sub

DGN File Search Paths

A MicroStation looks for DGN files in the folders indicated by configuration variable MS_DEF. There's more information about configuration variables and configuration files in MicroStation help.

MS_DEF points to a single folder or a list of folders. You would normally set MS_DEF per project, in your project configuration file or perhaps your user configuration file.

MicroStation Location

A MicroStation is the executing application that hosts your VBA run-time environment. Every application that hosts VBA has an Application object. The Application object has a Path property.

So, whether you're writing VBA for MicroStation, Excel, Word, or something else, you can find the host's location using Application.Path …

Dim path                                As String
path = Application.path
Debug.Print "Application.Path '" & path & "'"

MicroStation stores VBA projects in various locations. Those locations may be in one of MicroStation's workspace folders (e.g. \Bentley\MicroStation\WorkSpace\System\Vba) or in your own project-specific folders. See configuration variable MS_VBASEARCHDIRECTORIES in MicroStation VBA help for more information.

You may want to find the location of your VBA project at run-time.

VBA Project Location

A By default, VBA provides to way to find the location of the executing project at run-time. However, that information is available via the deftly-named Microsoft Visual Basic for Applications Extensibility 5.3. This is a VBA project, which you can find in C:\Documents and Settings\All Users\Application Data\Bentley\MicroStation\WorkSpace\System\Vba. Use the VBE IDE menu Tools|References to pop the VBA References dialog. Browse through the list to find the VBE Extensibility module and check the box to add it to your project …

References Dialog

Now you've referenced the VBE Extensibility module, you can use it in your code. In this example, we want to get the location of the currently-executing VBA code …

' ---------------------------------------------------------------------
'   ShowVbaProjectFolder
'   Shows how to find the folder where your VBA Project is located.
'   This is different to the Application.Path, which is the folder
'   where the executable that hosts the VBA run-time is located
'
'   The code that gets the VBA Project path requires a reference to
'   another project, 'Microsoft Visual Basic for Applications Extensibility 5.3'.
'   You reference another project using the VBA IDE menu Tools|References
' ---------------------------------------------------------------------
Sub ShowVbaProjectFolder()
    Dim path                                As String
    path = Application.path
    Debug.Print "Application.Path '" & path & "'"
    ' The next statement uses the VBE Project reference 
	Dim oVB                                 As VBProject
    Set oVB = ExecutingVBProject
    path = oVB.FileName
    Debug.Print "VBA Project path '" & path & "'"
End Sub

Find File

A The MDL mdlFile_find function searches for files in folders indicated by a configuration variable. When you use an MDL function, you first declare it in your VBA module. The declaration must be at the beginning of the module, before any VBA code …

Declare PtrSafe Function mdlFile_find Lib "stdmdlbltin.dll" ( _
    ByVal outname As String, _
    ByVal inname As String, _
    ByVal envvar As String, _
    ByVal iext As String) As Long

Here's a VBA function that wraps mdlFile_find. It returns a Boolean True or False value to indicate whether it was successful in finding the file. The optional warn parameter puts a message in MicroStation's message center if the file doesn't exist …

Public Function FindFile(ByRef found As String, _
	ByVal proposed As String,  _
	ByVal CfgVar As String,  _
	ByVal extension As String,  _
	Optional warn As Boolean = False) As Boolean
    FindFile = False
    On Error GoTo err_FindFile
    Dim buffer                              As String
    buffer = Space(254)
    Const SUCCESS                           As Long = 0
    If (SUCCESS = mdlFile_find(buffer, proposed, CfgVar, extension)) Then
         '   C-style string is NULL-terminated
        found = Left(buffer, InStr(buffer, Chr(0)) - 1)
        FindFile = True
    ElseIf (warn) Then
        Dim warning                         As String
        warning = "Unable to find file '" & proposed & "'" & _
                  " using configuration variable '" & CfgVar & "'"
        ShowMessage warning, warning, msdMessageCenterPriorityWarning, True
    End If
    Exit Function

err_FindFile:
    MsgBox "Error " & CStr(Err.Number) & ": " & Err.Description & vbNewLine & _
        "Caused by " & Err.Source, vbOKOnly Or vbCritical, "Error in modMDL:FindFile"
End Function

Here's a procecure that tests the VBA FindFile function …

Private Sub TestFileFind()
    Dim path                                As String
    Dim fileName                            As String
    fileName = "Bearing"
    Const Ext                               As String = "*.bas"
    Const CfgVar                            As String = "MS_MACRO"
    If (FindFile(path, fileName, CfgVar, Ext)) Then
        Debug.Print "Found '" & path & "'"
    End If
End Sub

Questions

Post questions about VBA to the MicroStation Programming Forum.