Results 1 to 9 of 9

Thread: Folder selection?? (RESOLVED)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26

    Folder selection?? (RESOLVED)

    How do I do folder selection (directory selection) using something like what common dialog will produce.

    using common dialog, I can onlt select files not the directory.

    VBA doesn't seems to have the DirList and DriveList controls that VB have to use.

    Thanks
    Last edited by weiyen; Jan 6th, 2004 at 08:56 PM.

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    try using the SHBrowseForFolder api
    -= a peet post =-

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    sample

    VB Code:
    1. Option Explicit
    2.  
    3. Public Type BrowseInfo
    4.      hwndOwner As Long
    5.      pIDLRoot As Long
    6.      pszDisplayName As Long
    7.      lpszTitle As Long
    8.      ulFlags As Long
    9.      lpfnCallback As Long
    10.      lParam As Long
    11.      iImage As Long
    12. End Type
    13.  
    14. Public Const BIF_RETURNONLYFSDIRS = 1
    15. Public Const BIF_NEWDIALOGSTYLE = &H40
    16.  
    17. Public Const MAX_PATH = 260
    18. Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    19. Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    20. Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    21. Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    22.  
    23. Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String
    24.      
    25.     'declare variables to be used
    26.      Dim iNull As Integer
    27.      Dim lpIDList As Long
    28.      Dim lResult As Long
    29.      Dim sPath As String
    30.      Dim udtBI As BrowseInfo
    31.  
    32.     'initialise variables
    33.      With udtBI
    34.         .hwndOwner = hwndOwner
    35.         .lpszTitle = lstrcat(sPrompt, "")
    36.         .ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE
    37.      End With
    38.  
    39.     'Call the browse for folder API
    40.      lpIDList = SHBrowseForFolder(udtBI)
    41.      
    42.     'get the resulting string path
    43.      If lpIDList Then
    44.         sPath = String$(MAX_PATH, 0)
    45.         lResult = SHGetPathFromIDList(lpIDList, sPath)
    46.         Call CoTaskMemFree(lpIDList)
    47.         iNull = InStr(sPath, vbNullChar)
    48.         If iNull Then sPath = Left$(sPath, iNull - 1)
    49.      End If
    50.  
    51.     'If cancel was pressed, sPath = ""
    52.      BrowseForFolder = sPath
    53.  
    54. End Function
    55.  
    56.  
    57. 'usage :
    58. Private Sub Form_Click()
    59.     MsgBox BrowseForFolder(Me.hWnd, "select folder")
    60. End Sub
    -= a peet post =-

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26
    I've tried the codes provided, howver, i was slapped with the following error.

    me.hwnd
    Compile Error:
    Method or data member not found

    Please advice.. Thanks

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    try using the form name instead of Me

    like

    VB Code:
    1. 'usage :
    2. Private Sub Form_Click()
    3.     MsgBox BrowseForFolder(Form1.hWnd, "select folder")
    4. End Sub
    -= a peet post =-

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26
    Thanks,
    That doesn't work either... However, I managed to get it working by using 0 in place of me.hwnd

    Another question:

    Is there a way to set the default folder?

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    yes there is.

    take a look at this great sample : http://www.planetsourcecode.com/vb/s...22276&lngWId=1

    -= a peet post =-

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26
    Thank you very much... you are way cool man!!

  9. #9
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    cool as the weather here at the mo... brrr....
    -= a peet post =-

Posting Permissions

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



Click Here to Expand Forum to Full Width