Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Dec 2000
    Location
    eastern Connecticut, Connecticut, USA
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Preset the folder in SHBrowseForFolder API (Excel 8 SR-2 VBA)

    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?

    ------------------------------------------------

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Preset the folder in SHBrowseForFolder API (Excel 8 SR-2 VBA)

    Did you track down all 7 declarations:

    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpbi As BROWSEINFO) As Long
    Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    Declare Function SHGetFolderLocation Lib "shell32.dll" Alias "SHGetFolderLocationA" (ByVal hwndOwner As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwReserved As Long, ppidl As Long) As Long
    Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, ppidl As Long) As Long
    Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)

    Plus these three constants:
    Const BFFM_SETSELECTION = &H466
    Const BFFM_INITIALIZED = 1
    Const CSIDL_DRIVES = &H11

    Plus all 4 user-defined types, in addition to the Dummy() function and the BrowseCallbackProc() function?
    Charlotte

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Preset the folder in SHBrowseForFolder API (Excel 8 SR-2 VBA)

    When you say...
    <hr>Is there a way in VBA to preset the folder using the SHBrowseForFolder API?<hr>
    Do you mean the folder that will open when you pop up the file open dialog? If so, I haven't figured it out (it is SO EASY in Word!). You would think this would work, but...

    <pre>Sub OpenSezMe()
    Dim strOldDefaultPath As String
    With Application
    strOldDefaultPath = .DefaultFilePath
    .DefaultFilePath = GetDirectory()
    If .DefaultFilePath = vbNullString Then
    .DefaultFilePath = strOldDefaultPath
    Exit Sub
    Else
    MsgBox .DefaultFilePath
    .Dialogs(xlDialogOpen).Show 'why c:my documents?!!
    .DefaultFilePath = strOldDefaultPath
    End If
    End With
    End Sub</pre>

    I'm stumped.

  4. #4
    2 Star Lounger
    Join Date
    Dec 2000
    Location
    eastern Connecticut, Connecticut, USA
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Preset the folder in SHBrowseForFolder API (Excel 8 SR-2 VBA)

    No - I'm not using the FileOpen or GetSaveAsFileName functions because I only want to put up a 'Choose Folder' function and both of those built in dialogs have unwanted side effects.

    Charlotte, in the reply above your's, gave me a bunch of additional stuff to run down.

    For something that I would have thought would have been somewhat simple, this is turning into a much bigger can of worms then I expected.

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Preset the folder in SHBrowseForFolder API (Excel 8 SR-2 VBA)

    When you aksed
    <hr>Is there a way in VBA to preset the folder using the SHBrowseForFolder API?<hr>
    I assumed you meant once you had the folder name. From your response, I gather that you want to start somewhere other than at the desktop in the browse for folder dialog itself.

    According to one <A target="_blank" HREF=http://msdn.microsoft.com/library/en-us/dncdev99/html/vc99c1.asp>article on MSDN</A>, "pidlRoot is the folder at the top of the directory tree displayed in the Browse For Folder dialog box. ... If you want to specify where the browsing starts, you can't just pass a string; you have to create an LPITEMIDLIST, and that's not a trivial task."

    But, you already knew that. <img src=/S/wink.gif border=0 alt=wink width=15 height=15> You can use the Shell32 object instead, but then constructing a full path is a hassle (you have to iteratively check the parentfolder.title). To play with that, add a reference to Microsoft Shell Controls and Automation and try this:

    <pre>Sub browsetest()
    Dim myshell As New Shell32.Shell, myFolder As Shell32.Folder
    On Error Resume Next
    Set myFolder = myshell.BrowseForFolder(0, "Find your folder...", 0, 0) 'starts at desktop
    'Set myFolder = myshell.BrowseForFolder(0, "Find your folder...", 0, "C:") 'starts at c:
    If Err <> 0 Then
    MsgBox Err.Description
    Set myshell = Nothing
    Exit Sub
    Else
    MsgBox myFolder.Title
    End If
    On Error GoTo 0
    Stop 'use View Locals Window to see how the properties work
    Set myFolder = Nothing
    Set myshell = Nothing
    End Sub</pre>

    Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •