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
Hi Jan,
I tried to run this code in Microstation mvba, following shows:
Any suggestion how to resolve this? I still don't quite get what "Scripting. " stands for.
Regards,
KZ
if you read the comments at beginning of code you will see
'if Compile error = "User-defined type not defined"'requires "MicroSoft Scripting Runtime" reference (Tools> References)
you need to add the reference in VBA IDE, it won't work without it
Hi JM,
As I am browsing the Microsoft Scripting Runtime in Microstation VBA Editor, I cannot find it.
However, I can find it in the vba editor in excel.
I try to manually load the scrrun.dll with the Browse, but after loading it, nothing happens in the available references.
Any suggestions?
Regards
the available references are not sorted 100% alphabetically (at least for me they are not)
try looking near the top of list, mine is in the top 10 if not already selected
if you can't find it in the list I don't know, I think it should be there though
Ugh,,,,, thank you!! Mine is on the tenth.
Kefan Zhou said:I am browsing the Microsoft Scripting Runtime in Microstation VBA Editor, I cannot find it
Once found, it appears at the top of the list.
Several of our example VBA projects use Microsoft Scripting Runtime. For example, Export Line Data to CSV.
Regards, Jon Summers LA Solutions