is this possible?
and if so how?
Printable View
is this possible?
and if so how?
VB Code:
Private 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 Private Const BIF_RETURNONLYFSDIRS = 1 Private Const MAX_PATH = 260 Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long) Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long Public Function BrowseForFolder(ByVal strInitDrive As String, ByVal lngHwndOwner As Long) As String 'Opens the browse for folder dialog Dim iNull As Integer, lpIDList As Long Dim sPath As String, udtBI As BrowseInfo With udtBI .hWndOwner = lngHwndOwner 'lstrcat appends the two strings and returns the memory address .lpszTitle = lstrcat(strInitDrive, "") 'Return only if the user selected a directory .ulFlags = BIF_RETURNONLYFSDIRS End With 'Show the 'Browse for folder' dialog lpIDList = SHBrowseForFolder(udtBI) If lpIDList Then sPath = String$(MAX_PATH, 0) 'Get the path from the IDList SHGetPathFromIDList lpIDList, sPath 'free the block of memory CoTaskMemFree lpIDList iNull = InStr(sPath, vbNullChar) If iNull Then sPath = Left$(sPath, iNull - 1) End If End If BrowseForFolder = sPath End Function
lemme see if i got this right... heh
BrowseForFolder is the end result and will return for example "C:\folder\folder2\" ?
sorry
me = VB super n00b
heh
basibally, that's exactly what it does :)
heh thanks :D
heh sorry aboiut this... just one more thing... just say i have a command button: named Button1 that i wanna use when clicked opens up that dialog box
how do i do that?
anyone? :(
Put the browse code in a module &
VB Code:
Private Sub Command1_Click() Dim strFolder As String strFolder = BrowserForFolder("C:\", Me.hWnd) MsgBox strFolder End Sub
thank you :D
sorry to do this to ya... i put in the code but it didnt work :(
Private 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
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Public Function BrowseForFolder(ByVal strInitDrive As String, ByVal lngHwndOwner As Long) As String
'Opens the browse for folder dialog
Dim iNull As Integer, lpIDList As Long
Dim sPath As String, udtBI As BrowseInfo
With udtBI
.hWndOwner = lngHwndOwner
'lstrcat appends the two strings and returns the memory address
.lpszTitle = lstrcat(strInitDrive, "")
'Return only if the user selected a directory
.ulFlags = BIF_RETURNONLYFSDIRS
End With
'Show the 'Browse for folder' dialog
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
'Get the path from the IDList
SHGetPathFromIDList lpIDList, sPath
'free the block of memory
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
BrowseForFolder = sPath
End Function
i put into a module
and
Dim strFolder As String
strFolder = BrowserForFolder("C:\", Me.hWnd)
MsgBox strFolder
i put into the button but i get a "sub or function not defined" error and it highlights this line:
strFolder = BrowseForFolder("C:\", Me.hWnd)
and this is highlighted with an yellow arrow
Private Sub ButRootFolder_Click()
am i doin it right?
also, do do you set the default path for the code above?
bumpage
The attached module is basically the same thing as what axion_sa has posted except it also lets you choose the default directory. The useage for it is:
The default directory is optional and will open to the c drive if left blank.VB Code:
Private Sub Command1_Click() Dim strPath As String strPath = GetFolderPath(Me, "C:\program files") MsgBox strPath End Sub
strPath = GetFolderPath(Me, "C:\program files")
the "C:\Program Files" is the default directory yes?
Yes. Change that to what ever you need or just don't use that parameter.
thanks :D
you typoed 'Browse'
VB Code:
Dim strFolder As String strFolder = [COLOR=red]Browser[/COLOR]ForFolder("C:\", Me.hWnd) MsgBox strFolder
that's why you were getting that error.
Suppose first time I selected folder "D:\Test". Now when I go for browsing again, I want that in browser window folder "D:\Test" is preselected and I should be able to either select a folder above that (i.e. its parent folder) or a folder below that (i.e. its sub folder). How to do this? i.e. when I go for browsing a folder earlier selected folder is shown preselected in this window.
Requires setting the lpfnCallback member of the BrowseInfo structure and then responding to messages it sends you. Suggest searching forum and/or google for: BrowseInfo lpfnCallback BFFM_SETSELECTION
Here is one example to get you started: http://vbcity.com/forums/t/80178.aspx
Thanks LaVolpe
With slight modifications it worked.