um i know how to select a file by adding the commondialog component, is selecting a directory similar? and can some tell me how ? thanks.
Printable View
um i know how to select a file by adding the commondialog component, is selecting a directory similar? and can some tell me how ? thanks.
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
should do what you want :)
If you want the user to select a file using the CommonDialog control add this code to your project and add a commonDialog control to your form
VB Code:
Private Function ShowFileDialog() As String With CommonDialog1 .CancelError = True .DialogTitle = "Select Datasource" .Filter = "Microsoft Access Databases(*.mdb)|*.mdb" .InitDir = App.Path On Error GoTo cancel_error 'Need this in case user hits Cancel button! .ShowOpen ShowFileDialog = .fileName End With Exit Sub cancel_error: If Err.Number <> cdlCancel Then 'an error occured somewhere MsgBox "Error #" & err.Number & vbcrlf & err.Description End If ShowFileDialog = "" 'Return an empty string End Function
If, on the other hand, you want to allow the user to choose a folder you can do this.
Add this to a module
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
and use it like this
VB Code:
Private Sub Command1_Click() MsgBox BrowseForFolder(Me.hWnd, "select folder") End Sub
Thanks :-) im shocking with API's, atleast XP does something now lol.
Damn you peet I copied that code from one of your posts LOL and then YOU beat me to it :)
hehe... I didn't have to search for my post.. :DQuote:
Originally posted by ae_jester
Damn you peet I copied that code from one of your posts LOL and then YOU beat me to it :)
I am using the nice code from Peet, my question:
How start with a specific folder instead of the root (C:\) ??
Wow! This code work great! Thanks :)
Wow! This code work great! Thanks :)
Thanks for letting us know that you have your answer. Are you aware that if you have JavaScript enabled you can do that easily by pulling down the Thread Tools menu and selecting the Mark Thread Resolved item? Also if someone has been particularly helpful you have the ability to affect their forum "reputation" by rating their post. Only those ratings that you give after you have 20 posts will actually count, but in all cases the person you rate will see your rating and know that you appreciate their help.