GetOpenFilename is a method which is also an attribute of FSO, this method is used in VBA to find a certain file with a file name and select it, the important factor in this method is the path of the file name provided to open it, we can either pass the path of the file name in the function or we can ask the user to present a file path to select it.
title | keywords | f1_keywords | ms.prod | api_name | ms.assetid | ms.date | localization_priority |
---|---|---|---|---|---|---|---|
vbaxl10.chm133142 | excel | 83931dc2-59b3-550b-6ce1-880413fd23d6 | Priority |
Displays the standard Open dialog box and gets a file name from the user without actually opening any files.
Syntax
expression.GetOpenFilename (FileFilter, FilterIndex, Title, ButtonText, MultiSelect)
expression A variable that represents an Application object.
Parameters
Name | Required/Optional | Data type | Description |
---|---|---|---|
FileFilter | Optional | Variant | A string specifying file filtering criteria. |
FilterIndex | Optional | Variant | Specifies the index numbers of the default file filtering criteria, from 1 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 | Variant | Specifies the title of the dialog box. If this argument is omitted, the title is 'Open.' |
ButtonText | Optional | Variant | Macintosh only. |
MultiSelect | Optional | Variant | True to allow multiple file names to be selected. False to allow only one file name to be selected. The default value is False. |
Return value
Variant
Remarks
This string passed in the FileFilter argument 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 Files of 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 FileFilter is omitted, this argument defaults to
'All Files (*.*),*.*'
.This method returns the selected file name 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 file names (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 folder.
Example
This example displays the Open dialog box, with the file filter set to text files. If the user chooses a file name, the code displays that file name in a message box.
[!includeSupport and feedback]
If your VBA procedure needs to ask the user for a filename, you could use the InputBox function and let the Excel user do some typing. An input box usually isn’t the best tool for this job, however, because most users find it difficult to remember paths, backslashes, filenames, and file extensions. In other words, it’s far too easy to screw up when typing a filename.
For a better solution to this problem, use the GetOpenFilename method of the Application object, which ensures that your code gets its hands on a valid filename, including its complete path. The GetOpenFilename method displays the familiar Open dialog box (a dead ringer for the dialog box Excel displays when you choose File → Open → Browse).
The GetOpenFilename method doesn’t actually open the specified file. This method simply returns the user-selected filename as a string. Then you can write code to do whatever you want with the filename.
The syntax for the GetOpenFilename method
The official syntax of the GetOpenFilename method is as follows:
GetOpenFilename method takes the optional arguments below.
Argument | What It Does |
---|---|
FileFilter | Determines the types of files that appear in the dialog box (for example, *.TXT). You can specify several filters for the user to choose from. |
FilterIndex | Determines which of the file filters the dialog box displays by default. |
Title | Specifies the caption for the dialog box’s title bar. |
ButtonText | Ignored (used only for the Macintosh version of Excel). |
MultiSelect | If True, the user can select multiple files. |
A GetOpenFilename example
The fileFilter argument determines what appears in the dialog box’s Files of Type drop-down list. This argument consists of pairs of file filter strings followed by the wildcard file filter specification, with commas separating each part and pair. If omitted, this argument defaults to the following:
Notice that this string consists of two parts, separated by a comma:
and
The first part of this string is the text displayed in the Files of Type drop-down list. The second part determines which files the dialog box displays. For example, *.* means all files.
The code in the following example opens a dialog box that asks the user for a filename. The procedure defines five file filters. Notice that the VBA line-continuation sequence is used to set up the Filter variable; doing so helps simplify this rather complicated argument.
Here is the dialog box Excel displays when you execute this procedure. The appearance may vary, depending on the version of Windows you use and the display options you’ve set.
The GetOpen Filename method displays a customizable dialog box and returns the selected file’s path and name. It does not open the file.
In a real application, you would do something more meaningful with the filename. For example, you might want to open it by using a statement such as this:
Notice that the FileName variable is declared as a Variant data type. If the user clicks Cancel, that variable contains a Boolean value (False). Otherwise, FileName is a string. Therefore, using a Variant data type handles both possibilities.