Public Type mOpenFilename
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Public Type mChooseColor
lStructSize As Long
hwndOwner As Long
hInstance As Long
rgbResult As Long
lpCustColors As String
Flags As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
'---- common dialog stuff :)
Public Declare Function apiGetOpenFilename Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pmOpenFilename As mOpenFilename) As Long
Public Declare Function apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pmOpenFilename As mOpenFilename) As Long
Public Declare Function apiGetChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pmChooseColor As mChooseColor) As Long
'---- Selects a/several file(s) using common dialogs
Public Function FileSelection(ByVal strInitDir As String, ByVal lngFlag As Long, ByVal strFilter As String, ByVal lngFilterIndex As Long, ByVal blnOpen As Boolean, Optional strTitle, Optional strFileExtension, Optional lngOwnerHwnd) As String
'---- Selects one or more filenames
'---- Requires
'---- inital directory to open in
'---- flags for opening
'---- usual flags are :
'---- &H200000 - use long filenames
'---- &H80000 - Explorer type window
'---- &H200 - multi select
'---- &H4 - hide read only option on box - v useful
'---- &H8 - force same dir as when opened
'---- &H1000 - file must exist - useful for opening
'---- usual I use : &H280004
'---- Returns a string holding either :
'---- nothing
'---- path and filename
'---- path chr$(0) filenames separated by chr$(0)
'---- NOTE : current max chars = 257 ... I think expand if neccessary
Dim strTemp As String
Dim lngReturn As Long, lngP As Long, lngO As Long
Dim cOpenFilename As mOpenFilename
On Error Resume Next
With cOpenFilename
'---- Default values according to another developer
.lStructSize = Len(cOpenFilename)
.hInstance = 0
.nFilterIndex = 1
.nFileOffset = 0
.lpstrFile = String(5000, 0)
.nMaxFile = Len(.lpstrFile) - 1
.lpstrFileTitle = .lpstrFile
.nMaxFileTitle = .nMaxFile
If Not IsMissing(strFileExtension) Then .lpstrDefExt = strFileExtension
'---- Bits like the common dialog control
If blnOpen Then
.lpstrTitle = "Open a file..."
Else
.lpstrTitle = "Save file as..."
End If
If Not IsMissing(strTitle) Then .lpstrTitle = strTitle
'---- messing
'---- default filter
'---- each filter is separated by a character of 0 - Name - filter - name filter (etc..)
'---- example :
' .lpstrFilter = "All Files" & Chr$(0) & "*.*" & Chr$(0) & "Text Files" & Chr$(0) & "*.txt;*.csv"
'---- replace filter with the selection chosen by the programmer...
.lpstrFilter = "All Files (*.*)" & Chr$(0) & "*.*"
lngO = 1
lngP = InStr(1, strFilter, "|")
If lngP > 0 Then
strTemp = ""
Do Until lngP = 0
strTemp = strTemp & IIf(Len(strTemp) > 0, Chr$(0), "") & Mid$(strFilter, lngO, lngP - lngO)
lngO = lngP + 1
lngP = InStr(lngP + 1, strFilter, "|")
Loop
strTemp = strTemp & Chr$(0) & Right$(strFilter, Len(strFilter) - lngO + 1)
Else
strTemp = strFilter
End If
.lpstrFilter = strTemp
.Flags = lngFlag
.hwndOwner = 0
If Not IsMissing(lngOwnerHwnd) Then .hwndOwner = lngOwnerHwnd
.lpstrInitialDir = strInitDir
End With
'---- is the dialog box an open or save?
If blnOpen Then
lngReturn = apiGetOpenFilename(cOpenFilename)
Else
lngReturn = apiGetSaveFileName(cOpenFilename)
End If
'---- send back the selected file(s)
If lngReturn = 0 Then
FileSelection = ""
Else
FileSelection = RemoveNonPChars(cOpenFilename.lpstrFile)
End If
End Function
Public Function RemoveNonPChars(ByVal strText As String) As String
'---- gets rid of the extra chr$(0)'s in the text
'---- by looking for two chr$(0)'s together (only happens at the end...)
'---- NOTE : only removes those at the end of the string - for multi means that you can get the filenames...
Dim lngP As Long
On Error Resume Next
RemoveNonPChars = " "
If Len(strText) = 0 Then Exit Function
lngP = InStr(1, strText, Chr$(0) & Chr$(0))
If lngP > 1 Then RemoveNonPChars = Left$(strText, lngP - 1)
If Not Err.Number = 0 Then
RemoveNonPChars = " "
Err.Clear
End If
End Function