To choose path by using DialogBox

Hello,
Do you know a DialogBox "FileOpen..." to open some file?
I need to implement the same functionality to choose some path.
How to do it?
Many thanks,
John.
[190 byte] By [John _Smith] at [2007-11-15 18:34:29]
# 1 Re: To choose path by using DialogBox
'Add from components the Microsoft Common Dialog Control
private Sub Command2_Click()
Dim ret, txtFileName as string
' set flags
CommonDialog1.Flags = cdlOFNHideReadOnly
' set filters
CommonDialog1.filter = "Bitmap Files (*.bmp)|*.bmp"
' Specify default filter
CommonDialog1.FilterIndex = 2
txtFileName = ""
me.CommonDialog1.CancelError = true
on error GoTo ErrHandler
me.CommonDialog1.ShowOpen
If Not UCase(Right(me.CommonDialog1.FileName, 4)) = ".bmp" then
txtFileName = ""
GoTo ErrHandler
End If
' Display name of selected file
txtFileName = me.CommonDialog1.FileName
Exit Sub

ErrHandler:
'User pressed the Cancel button
Err.Clear
Exit Sub
End Sub

Help us improve our answers by rating them.
MKSa at 2007-11-10 0:33:46 >
# 2 Re: To choose path by using DialogBox
This code allows to open file of .bmp type.

I'd like to choose PATH (not filename).

For example: to choose
"C:\Program Files\Microsoft Visual Studio\VB98\"
It's not important for me which files are placed
on these directory; just to choose this path.

Many thanks in advance.
John
John _Smith at 2007-11-10 0:34:41 >
# 3 Re: To choose path by using DialogBox
'Add from components the Microsoft Common Dialog Control
private Sub Command1_Click()
Dim ret, FilePath as string, pos as Long
' set flags
CommonDialog1.Flags = cdlOFNHideReadOnly
' set filters
CommonDialog1.Filter = "All Files (*.*)|*.*"
' Specify default filter
CommonDialog1.FilterIndex = 1
FilePath = ""
me.CommonDialog1.CancelError = true
on error GoTo ErrHandler
'Choose any file in any directory or type anything in the
'File dialog then hit Open and the path will be returned in FilePath
me.CommonDialog1.ShowOpen
pos = InStrRev(me.CommonDialog1.FileName, "\")
FilePath = Left(me.CommonDialog1.FileName, pos)
MsgBox FilePath, vbInformation
Exit Sub

ErrHandler:
'User pressed the Cancel button
Err.Clear
Exit Sub
End Sub


Help us improve our answers by rating them.
MKSa at 2007-11-10 0:35:44 >
# 4 Re: To choose path by using DialogBox
Hi
Here is a trick to replace these lines

pos = InStrRev(me.CommonDialog1.FileName, "\")
FilePath = Left(me.CommonDialog1.FileName, pos)

with
Filepath = Replace(Me.CommonDialog1.FileName, Me.CommonDialog1.FileTitle, "")

David Paulson
d.paulson at 2007-11-10 0:36:51 >
# 5 Re: To choose path by using DialogBox
option Explicit
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
Const BIF_RETURNONLYFSDIRS = 1
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
private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'KPDTeam@Allapi.net
Dim iNull as Integer, lpIDList as Long, lResult as Long
Dim sPath as string, udtBI as BrowseInfo

With udtBI
'set the owner window
.hWndOwner = me.hWnd
'lstrcat appends the two strings and returns the memory address
.lpszTitle = lstrcat("C:\", "")
'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

MsgBox sPath
End Sub
DSJ at 2007-11-10 0:37:50 >
# 6 Re: To choose path by using DialogBox
Don't use the commondialog control, but use an api call:

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

Const BIF_RETURNONLYFSDIRS = 1
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

Sub Test()

Dim iNull as Integer, lpIDList as Long, lResult as Long
Dim sPath as string, udtBI as BrowseInfo

With udtBI
'set the owner window
' .hWndOwner = me.hWnd
'lstrcat appends the two strings and returns the memory address
.lpszTitle = lstrcat("Select the location of the displays.", "")
'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
End Sub
VictorC at 2007-11-10 0:38:53 >