I believe I've created a function that likely replicates User @Alain's custom defined function, FindConstants("Message Here")
. The trick to getting VBA to call a directory path is to run through the Windows API. This function calls the file DLL to pull up a directory browser. It then saves the selection to DirPath()
. I'm not sure how to call these declarations on a 64 bit machine, and this is assuming you're running Windows 7. Here's what I
have so far:
Option Explicit
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) _
As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Function DirPath(Optional Msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
bInfo.pidlRoot = 0&
' Window title information
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If
' Directory information to return
bInfo.ulFlags = &H1
' Display the browsing dialog
x = SHBrowseForFolder(bInfo)
' Parse
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
DirPath = Left(path, pos - 1)
Else
DirPath = "" ' Saves no directory if nothing is selected!
End If
End Function
Here's a Sub to test the function above:
Sub Test()
Dim Msg As String
Msg = "Please select a template repository."
MsgBox GetDirectory(Msg)
End Sub
Sorry that's a little messy, I dunno how to clean up code blocks with Markdown... this isn't a complete answer and took me a while to get to because I'm not familiar with calling through the API. Plus, the MSDN is written for C+ in mind, which I do not know, so I had to find some tabled resources that equate C+ declarations with VBA ones (As Long vs. *FAR, et cetera). I'll keep the question open in case anyone can contribute or identify a function they spot in Alain's code as a add-on or as a well-known UDF. I'll edit this answer with more as I create and learn.