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
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Sub Command1_Click()
Dim tOFN As OPENFILENAME
tOFN.lStructSize = Len(tOFN)
tOFN.hwndOwner = Me.hWnd
tOFN.lpstrFilter = "Text Files (*.txt)" & vbNullChar & "*.txt"
tOFN.nMaxFile = 255
tOFN.nMaxFileTitle = 255
tOFN.lpstrFile = Space$(254)
tOFN.lpstrFileTitle = Space$(254)
'If OK was pressed
If GetOpenFileName(tOFN) Then
tOFN.lpstrFile = Left(tOFN.lpstrFile, InStr(1, tOFN.lpstrFile, vbNullChar))
MsgBox "You selected " & tOFN.lpstrFile
End If
End Sub