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
Hi,
Unknown said:It works, but I'm a novice at programming and I wonder if the code could be streamlined (better organized?)
It's an eternal question if a code is good enough or can be implemented in some better way ... but at the same time what does it mean "better way" is also important and tricky question ;-) To ask these questions are in my opinion one of mandatory conditions to become better programmer.
If you are serious about your development skills, I strongly recommend to read Clean Code: A Handbook of Agile Software Craftsmanship (don't mix up with The Clean Coder: A Code of Conduct for Professional Programmers from the same author). If you are beginner, the first 7 or 8 chapters are the most valuable for you and you will find plenty of ideas and examples how to refactor the code to be better (and also some ideas what "better" means). Don't care the examples are in Java, overall concepts and ideas are general.
Also webs like StackExchange (especially Programmers and Stack Overflow) can provide you plenty of advices and ideas what rules to follow to create better code (e.g. check this discussion).
Reading your code (and not thinking too much about it as VBA is not my favorite languge ;-), I recommend to follow a rule of a single responsibility, which comes from object oriented programming world, but can be used also in VBA: One function has to be responsible for one thing. Contrary to the rule, your code does many things (creates array of file names, iterate through it, opens file etc.) If you will break the code to more subs, you will also be closer to a rule every function should be short (even just a few lines).
With regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Answer Verified By: JJ Wutt