-
Hey there,
really need some help here.
Does anyone knows how to use VB to Browse for directories and files, like the "Explorer" in Windows?
My purpose is to select a file from a certain directories then, send it to users, just like the ICQ thing.
Can anyone help me out?
-
Simply you could use filelistbox, dirlistbox and drivelistbox. You could also develope a treestyle app like explorer using the tree. Commondialogbox is another simple solution.
-
Here you go, this should work:
First add a "CommonDialog" Control to your form from the "Microsoft Common Dialog Control 6.0" component (I called it "dlgOpenFile" in the following code), and then use this code.
Code:
'Open file dialog box
dlgOpenFile.Filter = "Executables (*.exe)|*.exe|" & _
"Text Files (*.txt)|*.txt|" & _
"All Files (*.*)|*.*" 'Change to match the files you want
dlgOpenFile.DialogTitle = "Select a file" 'Window Title
dlgOpenFile.Flags = _
cdlOFNFileMustExist + _
cdlOFNHideReadOnly + _
cdlOFNLongNames + _
cdlOFNExplorer
dlgOpenFile.CancelError = True
On Error Resume Next
dlgOpenFile.ShowOpen 'Open File Dialog Window
If Err.Number = cdlCancel Then 'The user canceled
Exit Sub
ElseIf Err.Number <> 0 Then 'Unknown error
MsgBox "Error " & Format$(Err.Number) & _
" selecting file." & vbCrLf & _
Err.Description
Exit Sub
End If
This will return a full path & Filename (i.e. "C:\AFolder\AFile.ext")
Hope this helps!
JMik
-
Paste this in the declarations section of a module:
Code:
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private 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
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Private Const BIF_RETURNONLYFSDIRS = &H1
and drop this somewhere in the module:
Code:
Public Function DialogSelectFolder(hOwner As Long, psTitle As String) As String
Dim udtBrowseInfo As BROWSEINFO, sPath As String
With udtBrowseInfo
.hOwner = hOwner
.pidlRoot = 0&
.lpszTitle = psTitle
.ulFlags = BIF_RETURNONLYFSDIRS
End With
sPath = Space$(512)
If SHGetPathFromIDList(SHBrowseForFolder(udtBrowseInfo), sPath) Then
DialogSelectFolder = Left(sPath, InStr(sPath, vbNullChar) - 1)
Else
DialogSelectFolder = ""
End If
End Function
It returns the selected folder, or "" if cancel was pressed.