I have created a VBA macro (code below) that opens the next .dgn file in the current folder. I created a custom tool and I run it from there. I created another, similar, macro that opens the "previous" drawing (not the previously opened dgn). It works, but I'm a novice at programming and I wonder if the code could be streamlined (better organized?) or if there is anything that I might have missed that will cause unexpected results. Any thoughts or comments would be appreciated. Thanks.
Sub OpenNextDgn() 'if Compile error = "User-defined type not defined" 'requires "MicroSoft Scripting Runtime" reference (Tools> References) On Error GoTo ErrorHandler ShowCommand "OpenNextDgn" Dim oFileSystem As Scripting.FileSystemObject Set oFileSystem = New Scripting.FileSystemObject Dim oFolder As Scripting.Folder Set oFolder = oFileSystem.GetFolder(ActiveDesignFile.Path) Dim DgnFiles() As String 'option base 1 Dim ArrayEmpty As Boolean ArrayEmpty = True Dim oFile As Scripting.File For Each oFile In oFolder.Files If Right(oFile.Name, 3) = "dgn" Then If ArrayEmpty Then ReDim DgnFiles(1 To 1) As String DgnFiles(1) = oFile.Name ArrayEmpty = False Else ReDim Preserve DgnFiles(1 To UBound(DgnFiles) + 1) As String DgnFiles(UBound(DgnFiles)) = oFile.Name End If End If Next oFile If UBound(DgnFiles) = 1 Then ShowPrompt "This is the only .dgn file in current folder" End ElseIf UBound(DgnFiles) > 1 Then Dim i As Integer Dim myCount As Integer For i = 1 To UBound(DgnFiles) myCount = myCount + 1 If DgnFiles(i) = ActiveDesignFile.Name Then Dim ActiveCount As Integer ActiveCount = myCount Dim NextCount As Integer NextCount = ActiveCount + 1 Exit For End If Next i If NextCount > UBound(DgnFiles) Then ShowPrompt "This is the last .dgn file in current folder" End Else myCount = 0 'reset For i = 1 To UBound(DgnFiles) myCount = myCount + 1 If myCount = NextCount Then Application.OpenDesignFile DgnFiles(i), False, msdV7ActionUpgradeToV8 CommandState.StartDefaultCommand Exit For End If Next i End If End If Exit Sub ErrorHandler: MsgBox Err.Description ShowCommand "OpenNextDgn" ShowPrompt "Error-macro failed" End Sub
If Right(oFile.Name, 3) = "dgn" Then
If
Right(oFile.Name, 3) =
"dgn"
Then
That tells you if a three-letter file extension matches 'dgn' exactly. What if the extension is 'DGN' (i.e. upper-case DGN not lower-case dgn)? What if the project uses DGN files having a different extension?
DGN
dgn
Read this article about how to Check a Design File with VBA. It shows a way to check any file to find if MicroStation can open it.
Regards, Jon Summers LA Solutions
Answer Verified By: JJ Wutt