Option Explicit
Public Const VER_PLATFORM_WIN32s = 0
Public Const VER_PLATFORM_WIN32_WINDOWS = 1
Public Const VER_PLATFORM_WIN32_NT = 2
Public Const OFN_ALLOWMULTISELECT As Long = &H200
Public Const OFN_CREATEPROMPT As Long = &H2000
Public Const OFN_ENABLEHOOK As Long = &H20
Public Const OFN_ENABLETEMPLATE As Long = &H40
Public Const OFN_ENABLETEMPLATEHANDLE As Long = &H80
Public Const OFN_EXPLORER As Long = &H80000
Public Const OFN_EXTENSIONDIFFERENT As Long = &H400
Public Const OFN_FILEMUSTEXIST As Long = &H1000
Public Const OFN_HIDEREADONLY As Long = &H4
Public Const OFN_LONGNAMES As Long = &H200000
Public Const OFN_NOCHANGEDIR As Long = &H8
Public Const OFN_NODEREFERENCELINKS As Long = &H100000
Public Const OFN_NOLONGNAMES As Long = &H40000
Public Const OFN_NONETWORKBUTTON As Long = &H20000
Public Const OFN_NOREADONLYRETURN As Long = &H8000& 'see comments
Public Const OFN_NOTESTFILECREATE As Long = &H10000
Public Const OFN_NOVALIDATE As Long = &H100
Public Const OFN_OVERWRITEPROMPT As Long = &H2
Public Const OFN_PATHMUSTEXIST As Long = &H800
Public Const OFN_READONLY As Long = &H1
Public Const OFN_SHAREAWARE As Long = &H4000
Public Const OFN_SHAREFALLTHROUGH As Long = 2
Public Const OFN_SHAREWARN As Long = 0
Public Const OFN_SHARENOWARN As Long = 1
Public Const OFN_SHOWHELP As Long = &H10
Public Const OFS_MAXPATHNAME As Long = 260
'OFS_FILE_OPEN_FLAGS and OFS_FILE_SAVE_FLAGS below
'are mine to save long statements; they're not
'a standard Win32 type.
Public Const OFS_FILE_OPEN_FLAGS = OFN_EXPLORER _
Or OFN_LONGNAMES _
Or OFN_CREATEPROMPT _
Or OFN_NODEREFERENCELINKS
Public Const OFS_FILE_SAVE_FLAGS = OFN_EXPLORER _
Or OFN_LONGNAMES _
Or OFN_OVERWRITEPROMPT _
Or OFN_HIDEREADONLY
Public Type OPENFILENAME
nStructSize As Long
hWndOwner As Long
hInstance As Long
sFilter As String
sCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
sFile As String
nMaxFile As Long
sFileTitle As String
nMaxTitle As Long
sInitialDir As String
sDialogTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
sDefFileExt As String
nCustData As Long
fnHook As Long
sTemplateName As String
pvReserved As Long
dwReserved As Long
FlagsEx As Long
End Type
Public OFN As OPENFILENAME
'windows-defined type OSVERSIONINFO
Public Type OSVERSIONINFO
OSVSize As Long 'size, in bytes, of this data structure
dwVerMajor As Long 'ie NT 3.51, dwVerMajor = 3; NT 4.0,
dwVerMajor = 4.
dwVerMinor As Long 'ie NT 3.51, dwVerMinor = 51; NT 4.0,
dwVerMinor= 0.
dwBuildNumber As Long 'NT: build number of the OS
'Win9x: build number of the OS in
low-order word.
'High-order word contains major & minor
ver nos.
PlatformID As Long 'Identifies the operating system
platform.
szCSDVersion As String * 128 'NT: string, such as "Service Pack 3"
'Win9x: 'arbitrary additional
information'
End Type
Public Declare Function GetVersionEx Lib "kernel32" _
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long
Public Declare Function GetOpenFileName Lib "comdlg32" _
Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Public Declare Function GetSaveFileName Lib "comdlg32" _
Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Public Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long) As Long
Public Function BrowseFor(sDesc As String, sExt As String, _
Optional lHwnd As Long, Optional sTitle As
String) _
As String
'used in call setup
Dim sFilters As String
'create a string of filters for the dialog
sFilters = sDesc & vbNullChar & sExt & vbNullChar & vbNullChar
With OFN
'size of the OFN structure
.nStructSize = IIf(IsWin2000Plus(), Len(OFN), Len(OFN) - 12)
If lHwnd Then
'window owning the dialog
.hWndOwner = lHwnd
End If
'filters (patterns) for the dropdown combo
.sFilter = sFilters
'index to the initial filter
.nFilterIndex = 2
'default filename, plus additional padding
'for the user's final selection(s). Must be
'double-null terminated
.sFile = sExt & Space$(1024) & vbNullChar & vbNullChar
'the size of the buffer
.nMaxFile = Len(.sFile)
'default extension applied to
'file if it has no extention
.sDefFileExt = sExt & vbNullChar & vbNullChar
'space for the file title if a single selection
'made, double-null terminated, and its size
.sFileTitle = vbNullChar & Space$(512) & vbNullChar & vbNullChar
.nMaxTitle = Len(OFN.sFileTitle)
'starting folder, double-null terminated
.sInitialDir = "c:\" & vbNullChar & vbNullChar
'the dialog title
.sDialogTitle = IIf(sTitle <> "", sTitle, "BrowseFor")
'default open flags and multiselect
.flags = OFS_FILE_OPEN_FLAGS Or _
OFN_ALLOWMULTISELECT
End With
'call the API
If GetOpenFileName(OFN) Then BrowseFor = OFN.sFile
End Function
Public Function IsWin2000Plus() As Boolean
'returns True if running Windows 2000 or better
Dim OSV As OSVERSIONINFO
OSV.OSVSize = Len(OSV)
If GetVersionEx(OSV) = 1 Then
IsWin2000Plus = (OSV.PlatformID = VER_PLATFORM_WIN32_NT) And _
(OSV.dwVerMajor = 5 And OSV.dwVerMinor >= 0)
End If
End Function
'Make a button called cmdBrowse to test:
Sub cmdBrowse_Click()
Dim sFile As String
'get the file
sFile = Trim(BrowseFor("ASCII Text file", "*.txt", _
Me.hwnd, "Select File List"))
End Sub
'In the above, you need to add the ability to grab a form's handle
'if you are using vba. Paste this code into the UserForm:
Private Declare Function GetActiveWindow Lib "user32" () As Long
Public Property Get hwnd() As Long
hwnd = GetActiveWindow()
End Property