|
-
Jan 2nd, 2004, 03:32 AM
#1
Thread Starter
Junior Member
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.
-
Jan 2nd, 2004, 03:35 AM
#2
-= B u g S l a y e r =-
try using the SHBrowseForFolder api
-
Jan 2nd, 2004, 03:36 AM
#3
-= B u g S l a y e r =-
sample
VB Code:
Option Explicit
Public Type BrowseInfo
hwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Public Const BIF_RETURNONLYFSDIRS = 1
Public Const BIF_NEWDIALOGSTYLE = &H40
Public Const MAX_PATH = 260
Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String
'declare variables to be used
Dim iNull As Integer
Dim lpIDList As Long
Dim lResult As Long
Dim sPath As String
Dim udtBI As BrowseInfo
'initialise variables
With udtBI
.hwndOwner = hwndOwner
.lpszTitle = lstrcat(sPrompt, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE
End With
'Call the browse for folder API
lpIDList = SHBrowseForFolder(udtBI)
'get the resulting string path
If lpIDList Then
sPath = String$(MAX_PATH, 0)
lResult = SHGetPathFromIDList(lpIDList, sPath)
Call CoTaskMemFree(lpIDList)
iNull = InStr(sPath, vbNullChar)
If iNull Then sPath = Left$(sPath, iNull - 1)
End If
'If cancel was pressed, sPath = ""
BrowseForFolder = sPath
End Function
'usage :
Private Sub Form_Click()
MsgBox BrowseForFolder(Me.hWnd, "select folder")
End Sub
-
Jan 2nd, 2004, 08:04 AM
#4
Thread Starter
Junior Member
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
-
Jan 2nd, 2004, 08:37 AM
#5
-= B u g S l a y e r =-
try using the form name instead of Me
like
VB Code:
'usage :
Private Sub Form_Click()
MsgBox BrowseForFolder(Form1.hWnd, "select folder")
End Sub
-
Jan 5th, 2004, 12:25 AM
#6
Thread Starter
Junior Member
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?
-
Jan 5th, 2004, 04:17 AM
#7
-= B u g S l a y e r =-
-
Jan 5th, 2004, 08:55 AM
#8
Thread Starter
Junior Member
Thank you very much... you are way cool man!!
-
Jan 6th, 2004, 01:28 AM
#9
-= B u g S l a y e r =-
cool as the weather here at the mo... brrr....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|