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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Preset the folder in SHBrowseForFolder API (Excel 8 SR-2 VBA)

    Author
    Topic
    #359503

    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?

    ————————————————

    Viewing 1 reply thread
    Author
    Replies
    • #539021

      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?

    • #539025

      When you say…
      [indent]


      Is there a way in VBA to preset the folder using the SHBrowseForFolder API?


      [/indent]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…

      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

      I’m stumped.

      • #539073

        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.

        • #539082

          When you aksed [indent]


          Is there a way in VBA to preset the folder using the SHBrowseForFolder API?


          [/indent]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 article on MSDN, “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. wink 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:

          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

          Good luck.

    Viewing 1 reply thread
    Reply To: Reply #539021 in Preset the folder in SHBrowseForFolder API (Excel 8 SR-2 VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel