iLogic: Excel Driven Folder Selection Dialog

iLogic: Excel Driven Folder Selection Dialog

greggeorgi
Contributor Contributor
726 Views
2 Replies
Message 1 of 3

iLogic: Excel Driven Folder Selection Dialog

greggeorgi
Contributor
Contributor

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.

Accepted solutions (1)
727 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

When used your code, everything seemed to work as expected, except that I was only able to select one folder.  I tried holding down the Ctrl key, but it didn't work.  Is there a trick to selecting multiple folders using this code?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

greggeorgi
Contributor
Contributor
Accepted solution

So multiselect is part of the filedialog capabilities which is why I included it, but I never actually tested it for folder selections until just now and I guess it doesn't work :/. Not a big deal for me, but it can be removed (or ignored).

 

From the documentation i just found at https://docs.microsoft.com/en-us/office/vba/api/office.filedialog.allowmultiselect, "This property has no effect on Folder Picker dialog boxes or SaveAs dialog boxes because users should never be able to select multiple files in these types of file dialog boxes."

 

Oh well, though I can see a use for it when choosing folders to open/search in, not for choosing a save location though.

 

I updated it for anyone interested.

''' <summary>
	''' Opens excel driven awesome folder browser dialog
	''' </summary>
	''' <param name="baseFolder">Starting Location, default to workspace folder</param>
	''' <returns>String representating the path of the chosen folder</returns>
	'use built in excel lib to access superior folder select dialog
	Function folderBrowseE(Optional baseFolder As String = "", Optional btnName As String = "Select Folder") As String
		If baseFolder = "" Then
			baseFolder = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\"
		End If

		Dim newExcelApp = CreateObject("Excel.Application")
		Dim ret As String
		 
		'see reference for options/settings
		'https://docs.microsoft.com/en-us/office/vba/api/overview/library-reference/filedialog-members-office
		With newExcelApp.FileDialog(4) 
			.ButtonName = btnName
			.InitialFileName = baseFolder
			.InitialView = 2
		    If .Show = -1
				ret = .SelectedItems(1) & "\" 'add slash for safety purposes, has no effect if extra, for other OS, change to "/"
			End If			
		End With 
		
		'may need to force kill process if it causes hangs?
		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 ret
	End Function

 

0 Likes