Visual Basic for Applications/File and Folder Dialogs

Summary
At times we need to access files and folders to provide input for procedures, and the code below will do this. They are not much different to the dialogs that Windows uses, and each of them works by returning a full path string to the chosen item. When a folder is selected, the returned string does not include the end backslash; the user needs to add that himself.

The two dialogs  SelectFolder  and  SelectFile  will work with both 32 bit and 64 bit version of MS Office, but the API procedure  BrowseFolder  is not intended for 64 bit working; it works only in 32 bit systems. For completeness, another version of the API for 64 bit systems has been added at the foot of the page. Although these two look a bit similar, it is important to choose the right one for your version of MS Office. All three can be run from the test procedure.

Just copy the entire code listing into a standard module for use, and comment out the API version that is unwanted, assuming that an API is used at all.

VBA Code Module
The default file-type listing that opens in SelectFile is decided by which of the Filters.Add code lines appears first in the sequence. For example, to have the All Files as your prefered listing, just move that line so that it immediately follows the Filters Clear line. Of course, the listing can also be changed by selecting the drop menu while the dialog is open.