GetOpenFilename Method

Applies To

Application Object.

Description

Displays the standard Open dialog box and gets a filename from the user without actually opening any files.

Syntax

object.GetOpenFilename(fileFilter, filterIndex, title, buttonText, multiSelect)

object

Required. The Application object.

fileFilter

Optional. A string specifying file filtering criteria.

In Microsoft Windows, this string consists of pairs of file filter strings followed by the MS-DOS wildcard file filter specification, with each part and each pair separated by commas. Each separate pair is listed in the File Type drop-down list box. For example, the following string specifies two file filters-text and addin: "Text Files (*.txt),*.txt,Add-In Files (*.xla),*.xla".

To use multiple MS-DOS wildcard expressions for a single file filter type, separate the wildcard expressions with semicolons. For example: "Visual Basic Files (*.bas; *.txt),*.bas;*.txt".

If omitted on Windows, this argument defaults to "All Files (*.*),*.*".

On the Apple Macintosh, this string is a list of comma-separated file type codes, ("TEXT,XLA,XLS4"). Spaces are significant and should not be inserted before or after the comma separators unless they are part of the file type code. If omitted, this argument defaults to all file types.

filterIndex

Optional. Microsoft Windows only (ignored on the Apple Macintosh). Specifies the index number of the default file filtering criteria from one to the number of filters specified in fileFilter. If this argument is omitted or greater than the number of filters present, the first file filter is used.

title

Optional. Microsoft Windows only (ignored on the Apple Macintosh). Specifies the dialog title. If this argument is omitted, the dialog title is "Open".

buttonText

Optional. Apple Macintosh only (ignored in Microsoft Windows). Specifies the text used for the Open button in the dialog box. If this argument is omitted, the button text is "Open".

multiSelect

Optional. If True, multiple filenames are allowed. If False or omitted, only a single filename can be selected.

Remarks

This method returns the selected filename or the name entered by the user. The returned name may include a path specification. If multiSelect is True, the return value is an array of the selected filenames (even if only one file name is selected). Returns False if the user cancels the dialog box.

This method may change the current drive or directory.

See Also

GetSaveAsFilename Method, Open Method.

Example

This example displays the Open dialog box, with the file filter set to text files. If the user chooses a filename, the code displays that filename in a message box.


fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
    MsgBox "Open " & fileToOpen
End If

This is the same example in Microsoft Excel for the Macintosh.


fileToOpen = Application.GetOpenFilename("TEXT")
If fileToOpen <> False Then
    MsgBox "Open " & fileToOpen
End If