
Option Explicit
Private Declare Function GetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" ( _
pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
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
Sub Open_Comdlg32()
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim strFilter As String
OpenFile.lStructSize = Len(OpenFile)
'// Define your wildcard string here
'// Note we pad the strings with Chr(0)
'// This indicates an end of a string
strFilter = "My Files (*T*.txt)" & Chr(0) & "*T*.txt" & Chr(0)
With OpenFile
.lpstrFilter = strFilter
.nFilterIndex = 1
.lpstrFile = String(257, 0)
.nMaxFile = Len(.lpstrFile) - 1
.lpstrFileTitle = .lpstrFile
.nMaxFileTitle = .nMaxFile
.lpstrInitialDir = "C:\"
.lpstrTitle = "My FileFilter Open"
.flags = 0
End With
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
'// User cancelled Do your thing
MsgBox "User cancelled"
Else
'// Do your thing
MsgBox "User selected" & ":=" & OpenFile.lpstrFile
'// So to Open the File use above Format
'// OpenFile gets part of OpenFile.lpstrFile
'// to the left of first Chr(0)
'// eg:
'//
Dim FileToOpen As String
FileToOpen = Application.WorksheetFunction.Clean(OpenFile.lpstrFile)
Workbooks.Open FileToOpen
End If
End Sub