Re: Browse for folder - Jim Rech's

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jim Rech (jrrech_at_hotmail.com)
Date: 08/12/04


Date: Thu, 12 Aug 2004 16:41:01 -0400

I've never been successful setting the root to anything other than a drive's
root. The follow streamlined code (no callback) sets the browse root to C:
but trying to set it lower results in a browse dialog that doesn't open.

Another problem with this solution, even if it did work, is that it uses an
undocumented Windows API call. No telling how reliable it is or if it will
be supported in the future.

-- 
Jim Rech
Excel MVP
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
'Main Browse for directory function
Declare Function SHBrowseForFolder Lib "shell32.dll" _
 Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
'Gets path from pidl
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
  Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) 
As Long
''UNDOCUMENTED!!!!
Declare Function SHSimpleIDListFromPath Lib "shell32" Alias "#162" (ByVal 
szPath As String) As Long
Sub Demo()
    Dim RetStr As String
    RetStr = GetDirectory("Choose path", "C:\") ''but C:\ABC doesn't work
    If RetStr <> "" Then MsgBox RetStr
End Sub
Function GetDirectory(Msg As String, TreeRoot As String) As String
    Dim bInfo As BROWSEINFO
    Dim Path As String
    Dim ItemIdentifierListAddress As Long
    Dim PathUnicode As String
    PathUnicode = StrConv(TreeRoot, vbUnicode)
    bInfo.pidlRoot = SHSimpleIDListFromPath(PathUnicode)
    If Not IsMissing(Msg) Then bInfo.lpszTitle = Msg
    ItemIdentifierListAddress = SHBrowseForFolder(bInfo)
    GetDirectory = GetPathFromID(ItemIdentifierListAddress)
End Function
'Converts a PIDL to a string
Function GetPathFromID(ID As Long) As String
    Dim Result As Boolean, Path As String * 255
    Result = SHGetPathFromIDList(ID, Path)
    If Result Then
        GetPathFromID = Left(Path, InStr(Path, Chr$(0)) - 1)
    Else
        GetPathFromID = ""
    End If
End Function


Relevant Pages

  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)
  • Auto Write Name and Merge across
    ... Dim Sheetname01 As String ... Dim WeekName01 As String ...
    (microsoft.public.excel.misc)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Help answer these 70-310 questions
    ... One argument is the string ... Dim output As New StringBuilder ... EmployeeLocations. ... You create a strongly named serviced component. ...
    (microsoft.public.cert.exam.mcsd)