I’m using the SHBrowseForFolder API to get a Folder choice from the user using a construction similar to the snippet below. There are a couple of examples floating around the web (vbapi.com/ref/s/shbrowseforfolder.html for one) that use a ‘Dummy Function’ to enable use of the AddressOf operator to point to a Function named BrowseCallBackProc that will accept a ‘preset argument’. The VBE will not let me use AddressOf – says it’s not active in this VBA. Is there a way in VBA to preset the folder using the SHBrowseForFolder API?
Declare Function SHBrowseForFolder Lib “shell32.dll”( ByRef lpbi As BROWSEINFO) As Long
Declare Function SHGetPathFromIDList Lib “shell32″(ByVal pidList As Long, ByVal lpBuffer As String) As Long
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
Function GetDirectory(Optional msg) As String
‘needs api calls SHBrowseForFolder and SHGetPathFromIDList
‘needs type statement Browseinfo
Dim bInfo As BROWSEINFO
Dim path As String
Dim R As Long, X As Long, pos As Integer
bInfo.pidlRoot = 0& ‘ Root folder = Desktop
If IsMissing(msg) Then ‘ Title in the dialog
bInfo.lpszTitle = “Select a folder.”
Else
bInfo.lpszTitle = msg
End If
bInfo.ulFlags = &H1 ‘ Type of directory to return
X = SHBrowseForFolder(bInfo) ‘ Display the dialog
path = Space$(512) ‘ Parse the result
R = SHGetPathFromIDList(ByVal X, ByVal path)
If R Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos – 1)
Else
GetDirectory = “”
End If
End Function
Sub TestGetDir()
Dim strMsg As String
strMsg = “hello world”
MsgBox GetDirectory(strMsg), , strMsgBoxTitle
End Sub
(vbapi.com/ref/s/shbrowseforfolder.html for one) that demonstrate how to ‘preset’ this with a starting folder. Problem is that all of the examples I’ve seen use a ‘Dummy Function’ to enable use of the AddressOf operator for another function. My VBE will not allow me to use that function. Is there a way to make a folder preselection in SHBrowseForFolder in VBA?
————————————————