MicroStation VBA batch save to different file type using VBA

Hi, 

I'm trying to automate a task for a colleague. I use VBA in other applications but do not use MicroStation so I may be going about this the wrong way. 

I have a number of DGN files in a folder. I'm trying to loop through each file to save as .dwg and rename file to include this file type. This happens once a quarter and is done manually at present. 

I recorded a macro while saving a .dgn file to .dwg and tried to modify this. However, I'm not able to create the new files. The original macro option worked for a single file but not while looping through the files. 

Full code below. Note, for testing, once I open MicroStation, I open the Macro under Utilities tab and click edit icon to open VBA. From there I I'm simply running the module BmrMacroExportTest() below. 

Question: Is VBA the option to batch save files to different formats? If so, can someone point out the error in my code? If I can get this to work, I will also output to .dxf and give user input option to select folder location as it updates per quarter. 

Thanks in advance for any assistance. 

Module: modMacroExportTest

Sub BmrMacroExportTest()
    Dim startPoint As Point3d
    Dim point As Point3d, point2 As Point3d
    Dim lngTemp As Long
    Dim oMessage As CadInputMessage

    Dim modalHandler As New BmrMacroExportTestModalHandler
    AddModalDialogEventsHandler modalHandler

'   The following statement opens modal dialog "Save As"

'   Send a keyin that can be a command string
    CadInputQueue.SendKeyin "DIALOG SAVEAS "

    RemoveModalDialogEventsHandler modalHandler
    CommandState.StartDefaultCommand
End Sub

Class Module: bmrMacroExportTestModalHandler

Implements IModalDialogEvents
Private Sub IModalDialogEvents_OnDialogClosed(ByVal DialogBoxName As String, ByVal DialogResult As MsdDialogBoxResult)

End Sub

Private Sub IModalDialogEvents_OnDialogOpened(ByVal DialogBoxName As String, DialogResult As MsdDialogBoxResult)

    Dim strInputPath As String
    Dim strInputFile As String
    Dim strOutputFile As String
    Dim strOutputFormat As String
    Dim blnContinue As Boolean

    strInputPath = "DRIVE:\...\FINALFOLDER\"
    
    ' Loop through input files in input path
    strInputFile = Dir(strInputPath & "*.dgn")

    Do While strInputFile <> ""
        ' Set output file name and format

        strOutputFile = Left(strInputFile, Len(strInputFile) - 4)
        strOutputFile = Replace(strOutputFile, "DGN8", "DWG")
        
        ' Export to DWG
        strOutputFormat = "DWG"
    
        If DialogBoxName = "Save As" Then
            Call SaveNewFile(strInputPath, strOutputFile & "." & LCase(strOutputFormat))
        End If  '
     
    	' Move to next input file
        strInputFile = Dir()
       
    Loop
End Sub

Private Sub SaveNewFile(strPath As String, strNewFile As String)
        
            CadInputQueue.SendKeyin "MDL COMMAND MGDSHOOK,fileList_setDirectoryCmd " & strPath 
            
            CadInputQueue.SendKeyin "MDL COMMAND MGDSHOOK,fileList_setFileNameCmd " & strNewFile
            
            '   Remove the following line to let the user close the dialog box.
            DialogResult = msdDialogBoxResultOK
End Sub

  • Is VBA the option to batch save files to different formats?

    See the SaveAs method in VBA Help: Saves the current file under a different file name and, optionally, in a different file format.

    Enum MsdDesignFileFormat includes msdDesignFileFormatDWG.  VBA help is delivered with MicroStation: here's how to find it.

     
    Regards, Jon Summers
    LA Solutions

  • Hi Jon, 

    Many thanks for that. The location of this help was not apparent! I tried the SaveAs option briefly but I don't think it's the best approach. 

    I have since found the Tools - Batch Converter route which does what it says on the tin. This is being tested at the moment so if that works, I won't need to proceed with the VBA option. As a newbie in MicroStation VBA, it does look a bit cumbersome and more difficult to get started with than VBA elsewhere. Presumably this has a lot to do with my lack of knowledge in MicroStation itself. 

    Thanks,

    Donal 

  • Batch Converter really is the better tool for the job, IMO. No need to recreate a tool that already works.

    VBA for MicroStation isn't bad - it's just pretty MicroStation specific. If you aren't familiar with the software, and how it works in daily use, I can imagine that it would be difficult to wrap your head around what a program might need to work. I use Excel, but I'm not very proficient, and I find Excel VBA challenging.

    What I like about the MicroStation VBA help is that it includes many code snips to illustrate how various tools, properties and methods function. That was very helpful to me when I was learning to code.

    MaryB

    Power GeoPak 08.11.09.918
    Power InRoads 08.11.09.918
    OpenRoads Designer 2021 R2