Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Standard windows dialogue for selecting a folder?

Status
Not open for further replies.

Jonathan

Programmer
Mar 19, 1999
116
0
0
GB
I am using the Windows API in my application to allow the user to easily select files to open using the standard windows Open File box. I also need to provide some means of selecting a folder. An example of what I want can be seen by going to the start menu, clicking Find - Files or Folders and clicking the Browse... button next to the Look In combo.<br>
<br>
Does anyone know if there is a Win API function or other easy method for achieving this (preferably without getting new ActiveX controls)<br>
<br>
Cheers<br>
<br>
Jonathan <br>

 
The following class allows you to specify the starting folder and filter(s). For multiple choice folders I would suggest a local DB (.mdb?) to save your favorites.<br>
<br>
<br>
Option Explicit<br>
<br>
Private Type OPENFILENAME<br>
StructSize As Long<br>
hwndOwner As Long<br>
hInstance As Long<br>
Filter As String<br>
CustomFilter As String<br>
MaxCustFilter As Long<br>
FilterIndex As Long<br>
FileFilterIndex As String<br>
MaxFile As Long<br>
FileTitle As String<br>
MaxFileTitle As Long<br>
InitialDir As String<br>
Title As String<br>
flags As Long<br>
FileOffset As Integer<br>
FileExtension As Integer<br>
DefExt As String<br>
CustData As Long<br>
Hook As Long<br>
TemplateName As String<br>
End Type<br>
<br>
Private Declare Function GetOpenFileName Lib &quot;comdlg32.dll&quot; Alias &quot;GetOpenFileNameA&quot; (pOpenfilename _<br>
As OPENFILENAME) As Long<br>
<br>
Private Declare Function GetSaveFileName Lib &quot;comdlg32.dll&quot; Alias &quot;GetSaveFileNameA&quot; (pOpenfilename _<br>
As OPENFILENAME) As Long<br>
<br>
Private DefaultExt_ As String<br>
Private DialogTitle_ As String<br>
Private FileName_ As String<br>
Private FileTitle_ As String<br>
Private InitialDir_ As String<br>
Private Filter_ As String<br>
Private FilterIndex_ As Integer<br>
Private Flags_ As FileFlags<br>
Private MaxFileSize_ As Integer<br>
Private hWndParent_ As Long<br>
<br>
Private Const MaxFileLength As Integer = 260<br>
<br>
Public Enum FileFlags<br>
FileReadOnly = &H1<br>
FileOverWritePrompt = &H2<br>
FileHideReadOnly = &H4<br>
FileNoChangeDir = &H8<br>
FileShowHelp = &H10<br>
FileEnableHook = &H20<br>
FileEnableTemplate = &H40<br>
FileEnableTemplateHandle = &H80<br>
FileNoValidate = &H100<br>
FileAllowMultiSelect = &H200<br>
FileExtensionDifferent = &H400<br>
FilePathMustExist = &H800<br>
FileFileMustExist = &H1000<br>
FileCreatePrompt = &H2000<br>
FileShareAware = &H4000<br>
FileNoReadOnlyReturn = &H8000<br>
FileNoTestFileCreate = &H10000<br>
FileNoNetworkButton = &H20000<br>
FileExplorer = &H80000<br>
FileLongnames = &H200000<br>
End Enum<br>
<br>
<br>
Public Property Get DefaultExt() As String<br>
DefaultExt = DefaultExt_<br>
End Property<br>
<br>
Public Property Let DefaultExt(ByVal strValue As String)<br>
DefaultExt_ = strValue<br>
End Property<br>
<br>
Public Property Get DialogTitle() As String<br>
DialogTitle = DialogTitle_<br>
End Property<br>
<br>
Public Property Let DialogTitle(ByVal strValue As String)<br>
DialogTitle_ = strValue<br>
End Property<br>
<br>
Public Property Get FileName() As String<br>
FileName = FileName_<br>
End Property<br>
<br>
Public Property Let FileName(ByVal strValue As String)<br>
FileName_ = strValue<br>
End Property<br>
<br>
Public Property Get FileTitle() As String<br>
FileTitle = FileTitle_<br>
End Property<br>
<br>
Public Property Let FileTitle(ByVal strValue As String)<br>
FileTitle_ = strValue<br>
End Property<br>
<br>
Public Property Get Filter() As String<br>
Filter = Filter_<br>
End Property<br>
<br>
Public Property Let Filter(ByVal FilterValue As String)<br>
Filter_ = FilterValue<br>
End Property<br>
<br>
Public Property Get FilterIndex() As Integer<br>
FilterIndex = FilterIndex_<br>
End Property<br>
<br>
Public Property Let FilterIndex(ByVal intValue As Integer)<br>
FilterIndex_ = intValue<br>
End Property<br>
<br>
Public Property Get flags() As FileFlags<br>
flags = Flags_<br>
End Property<br>
<br>
Public Property Let flags(ByVal eValue As FileFlags)<br>
Flags_ = eValue<br>
End Property<br>
<br>
Public Property Get hWndParent() As Long<br>
hWndParent = hWndParent_<br>
End Property<br>
<br>
Public Property Let hWndParent(ByVal lngValue As Long)<br>
hWndParent_ = lngValue<br>
End Property<br>
<br>
Public Property Get InitialDir() As String<br>
InitialDir = InitialDir_<br>
End Property<br>
<br>
Public Property Let InitialDir(ByVal strValue As String)<br>
InitialDir_ = strValue<br>
End Property<br>
<br>
Public Property Get MaxFileSize() As Integer<br>
MaxFileSize = MaxFileSize_<br>
End Property<br>
Public Property Let MaxFileSize(ByVal intValue As Integer)<br>
MaxFileSize_ = intValue<br>
End Property<br>
<br>
Public Function Show(fOpen As Boolean) As Boolean<br>
Dim of As OPENFILENAME<br>
Dim strChar As String * 1<br>
Dim intCounter As Integer<br>
Dim strTemp As String<br>
<br>
On Error GoTo PROC_ERR<br>
<br>
' Initialize the OPENFILENAME type<br>
of.Title = DialogTitle_ & &quot;&quot;<br>
of.flags = Flags_<br>
of.DefExt = DefaultExt_ & &quot;&quot;<br>
of.StructSize = LenB(of)<br>
of.Filter = Filter_ & &quot;¦¦&quot;<br>
of.FilterIndex = FilterIndex_<br>
<br>
' To make Windows-style filter, replace pipes with nulls<br>
For intCounter = 1 To Len(Filter_)<br>
strChar = Mid$(Filter_, intCounter, 1)<br>
If strChar = &quot;¦&quot; Then<br>
strTemp = strTemp & vbNullChar<br>
Else<br>
strTemp = strTemp & strChar<br>
End If<br>
Next<br>
<br>
' Put double null at end<br>
strTemp = strTemp & vbNullChar & vbNullChar<br>
of.Filter = strTemp<br>
<br>
' Pad file and file title buffers to maximum path length<br>
strTemp = FileName_ & String$(MaxFileLength - Len(FileName_), 0)<br>
of.FileFilterIndex = strTemp<br>
of.MaxFile = MaxFileLength<br>
<br>
strTemp = FileTitle_ & String$(MaxFileLength - Len(FileTitle_), 0)<br>
of.FileTitle = strTemp<br>
of.InitialDir = InitialDir_<br>
of.MaxFileTitle = MaxFileLength<br>
of.hwndOwner = hWndParent_<br>
<br>
' If fOpen is true, show the Open file dialog, otherwise show the Save dialog<br>
If fOpen Then<br>
If GetOpenFileName(of) Then<br>
Show = True<br>
' Assign property variables to appropriate values<br>
FileName_ = TrimNulls(of.FileFilterIndex)<br>
FileTitle_ = TrimNulls(of.FileTitle)<br>
Else<br>
Show = False<br>
End If<br>
Else<br>
If GetSaveFileName(of) Then<br>
Show = True<br>
' Assign property variables to appropriate values<br>
FileName_ = TrimNulls(of.FileFilterIndex)<br>
FileTitle_ = TrimNulls(of.FileTitle)<br>
Else<br>
Show = False<br>
End If<br>
End If<br>
<br>
PROC_EXIT:<br>
<br>
<br>
Exit Function<br>
<br>
PROC_ERR:<br>
<br>
<br>
MsgBox &quot;Error: &quot; & Err.Number & &quot;. &quot; & Err.Description, , &quot;Show&quot;<br>
Resume PROC_EXIT<br>
<br>
End Function<br>
<br>
Private Function TrimNulls(ByVal strIn As String) As String<br>
Dim intPos As Integer<br>
On Error GoTo PROC_ERR<br>
intPos = InStr(strIn, vbNullChar)<br>
If intPos = 0 Then<br>
TrimNulls = strIn<br>
Else<br>
If intPos = 1 Then<br>
TrimNulls = &quot;&quot;<br>
Else<br>
TrimNulls = Left$(strIn, intPos - 1)<br>
End If<br>
End If<br>
<br>
PROC_EXIT:<br>
<br>
<br>
Exit Function<br>
<br>
PROC_ERR:<br>
<br>
<br>
MsgBox &quot;Error: &quot; & Err.Number & &quot;. &quot; & Err.Description, , &quot;TrimNulls&quot;<br>
Resume PROC_EXIT<br>
<br>
End Function<br>

 
Thanks<br>
<br>
I already have some similar code, however I need to allow the user to select a folder, not a file. Can this be done using the standard windows Open box?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top