Microsoft Office Tutorials and References
In Depth Information
The Excel GetOpenFilename Method
The FilterIndex argument specifies which FileFilter is the default, and the Title
argument is text that is displayed in the title bar. If the MultiSelect argument is True , the user
can select multiple files, all of which are returned in an array.
The following example prompts the user for a filename. It defines five file filters.
Sub GetImportFileName()
Dim Filt As String
Dim FilterIndex As Integer
Dim Title As String
Dim FileName As Variant
‘ Set up list of file filters
Filt = “Text Files (*.txt),*.txt,” & _
“Lotus Files (*.prn),*.prn,” & _
“Comma Separated Files (*.csv),*.csv,” & _
“ASCII Files (*.asc),*.asc,” & _
“All Files (*.*),*.*”
‘ Display *.* by default
FilterIndex = 5
‘ Set the dialog box caption
Title = “Select a File to Import”
‘ Get the file name
FileName = Application.GetOpenFilename _
(FileFilter:=Filt, _
FilterIndex:=FilterIndex, _
Title:=Title)
‘ Exit if dialog box canceled
If FileName = False Then
MsgBox “No file was selected.”
Exit Sub
End If
‘ Display full path and name of the file
MsgBox “You selected “ & FileName
End Sub
Figure 12-8 shows the dialog box that appears when this procedure is executed and the user
selects the Comma Separated Files filter.
 
Search JabSto ::




Custom Search