Microsoft Office Tutorials and References
In Depth Information
The Excel GetOpenFilename Method
The Excel GetOpenFilename Method
If your application needs to ask the user for a filename, you can use the InputBox function. But
this approach is tedious and error-prone because the user must type the filename (with no
browsing ability). A better approach is to use the GetOpenFilename method of the Application
object, which ensures that your application gets a valid filename (as well as its complete path).
This method displays the normal Open dialog box, but it does not actually open the file specified.
Rather, the method returns a string that contains the path and filename selected by the user.
Then you can write code to do whatever you want with the filename.
The syntax for the GetOpenFilename method is as follows (all arguments are optional):
ApplicationGetOpenFilename(FileFilter, FilterIndex, Title, ButtonText,
MultiSelect)
h FileFilter : Optional. A string specifying file-filtering criteria.
h FilterIndex : Optional. The index number of the default file-filtering criteria.
h Title : Optional. The title of the dialog box. If omitted, the title is Open .
h ButtonText : For Macintosh only.
h MultiSelect : Optional. If True , you can select multiple filenames. The default value is
False .
The FileFilter argument determines what file types appear in the dialog box’s Files of Type
drop-down list. The argument consists of pairs of file filter strings followed by the wildcard file
filter specification, with each part and each pair separated by commas. If omitted, this argument
defaults to the following:
“All Files (*.*),*.*”
Notice that the first part of this string ( All Files (*.*) ) is the text displayed in the Files of
Type drop-down list. The second part ( *.* ) actually determines which files are displayed.
The following instruction assigns a string to a variable named Filt . You can then use this string
as a FileFilter argument for the GetOpenFilename method. In this case, the dialog box
will allow the user to select from four different file types (plus an All Files option). Notice that I
used VBA’s line continuation sequence to set up the Filt variable; doing so makes it much
easier to work with this rather complicated argument.
Filt = “Text Files (*.txt),*.txt,” & _
“Lotus Files (*.prn),*.prn,” & _
“Comma Separated Files (*.csv),*.csv,” & _
“ASCII Files (*.asc),*.asc,” & _
“All Files (*.*),*.*”
 
Search JabSto ::




Custom Search