- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, I made a function to search for a folder. Inventor's built in folder dialog is the outdated version, and I couldn't get the better (vista+?) version to work no matter how hard I tried. (Within iLogic environment and no 3rd party, I don't have access to addin creation or C#, same result with VBA, also I was interested in simplicity since I am by no means knowledgeable in .net/MS API/VB. I was unable to figure out if/how IFileDialog could be used within iLogic and it seemed too complex for my liking)
My company uses MS Office which means it has excel which has a nice folder browser built in, if set up properly, so i came up with this which works perfectly for my needs
'Creates Excel Object to use its folder browser method which is better than Inventor's
'Returns ArrayList in case multiSelect is true
Function folderBrowseE(Optional multiSelect As Boolean = False, Optional baseFolder As String = "", Optional btnName As String = "Select Folder") As ArrayList
If baseFolder = "" then
baseFolder = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\" 'Adding a backslash here allows the folder itself to be chosen
End If
Dim newExcelApp = CreateObject("Excel.Application")
Dim returnList = New ArrayList()
'see reference for options/settings, not all are included below
'https://docs.microsoft.com/en-us/office/vba/api/overview/library-reference/filedialog-members-office
With newExcelApp.FileDialog(4) '#4 represents folder selection
.AllowMultiSelect = multiSelect
.ButtonName = btnName
.InitialFileName = baseFolder 'Can be a folder or file with wild cards, though this is a folder selector so it would be pointless to specify files.
.InitialView = 2 'Details view
If .Show = -1
For itemCount = 1 To .SelectedItems.Count
returnList.Add(.SelectedItems(itemCount) & "\") 'add slash for safety purposes
Next itemCount
End If
End With
'may need to force kill process if it causes hangs - unsure?
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(newExcelApp) 'dispose, excel process terminates when rule ends so may be uneccessary...
newExcelApp = Nothing 'unncessary, GC would collect anyways after return....
Return returnList
End Function
Note I made multi select an, option, but I never did test it with folders....
and an example of usage, note this is where the error checking happens....
Dim folder As String
Try
folder = folderBrowseE().item(0) 'NOTE ADDS '\' TO END OF PATH
Catch
Exit Sub
End Try
Any feedback/advice/improvements are appreciated!, and I hope this may be useful to somebody as a simpler replacement to Inventor's folder selection options.
Note: Inventor's file dialog is pretty good, or if more functionality is needed, System.Windows.Forms.OpenFileDialog is good enough.
Solved! Go to Solution.