Multiple Apps

Browse For Folder (using FileDialog method)

Ease of Use

Easy

Version tested with

Office 2002+ 

Submitted by:

Ken Puls

Description:

This function will allow a user to Browse for a folder (at a user defined starting point if desired.) It is easy to use, as the code requires no API's or special references. This function will NOT work with Office 97 or Office 2000. 

Discussion:

There may be times when a user needs to point a procedure to a particular folder, say to open of save a file from/to. This macro allows the user to browse to the folder, and returns the full file path to the folder selected, or an empty string if cancelled. This is very similar in effect to http://vbaexpress.com/kb/getarticle.php?kb_id=284, but with one important difference. Unlike the other entry, you CAN browse above the optional "OpenAt" directory. 

Code:

instructions for use

			

Function GetFolderName(Optional OpenAt As String) As String Dim lCount As Long GetFolderName = vbNullString With Application.FileDialog(msoFileDialogFolderPicker) .InitialFileName = OpenAt .Show For lCount = 1 To .SelectedItems.Count GetFolderName = .SelectedItems(lCount) Next lCount End With End Function

How to use:

  1. Copy above code.
  2. In application press Alt + F11 to enter the VBE.
  3. Press Ctrl + R to show the Project Explorer.
  4. Right-click desired file on left (in bold).
  5. Choose Insert -> Module.
  6. Paste code into the right pane.
  7. Press Alt + Q to close the VBE.
  8. Save document before any other changes
 

Test the code:

  1. In application press Alt + F11 to enter the VBE.
  2. Press Ctrl + G to show the Immediate window
  3. To test with no default, type the following, then press Enter: ?getfoldername
  4. You should be prompted with the browse window. Upon completion, it should type the name of the folder you selected in the Immediate window under what you typed above (unless you clicked cancel, where you'll get a blank line).
  5. To test with a default, type the following, then press Enter: ?getfoldername("C:\temp")
  6. You should be prompted with the browse window already in the "Temp" folder. Upon completion, it should type the name of the folder you selected in the Immediate window under what you typed above (unless you clicked cancel).
 

Sample File:

GetFolder.zip 9.27KB 

Approved by mdmackillop


This entry has been viewed 209 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express