The Get OpenFilename Method

If your VBA procedure needs to prompt the user for a filename, you could use the InputBox function. An InputBox usually isn't the best tool for this job, however, because most users find it difficult to remember paths and directory names, and typographic errors often result.

For a better solution to this problem, use the GetOpenFilename method of the Application object, which ensures that your application gets a valid filename, including its complete path. The GetOpenFilename method displays the familiar Open dialog box (the same dialog box Excel displays when you choose FileOOpen). The GetOpenFilename method doesn't actually open the specified file. This method simply returns the user-selected filename. Then you can write code to do whatever you want with the filename.

The syntax

The official syntax for this method is as follows:

object.GetOpenFilename([fileFilter], [filterlndex], [title], [buttonText], [multiSelect])

The GetOpenFileName method takes the optional arguments shown in Table 15-5.

Table 15-5

GetOpenFileName Method Arguments


What It Does


Determines the types of files that appear in the dialog box

(for example, *.TXT). You can specify several different fil

ters from which the user can choose.


Determines which of the file filters the dialog box displays

by default.


Specifies the caption for the dialog box's title bar.


Ignored (used only for the Macintosh version of Excel).


If True, the user can select multiple files.

Was this article helpful?

0 0


  • genet elias
    How to use getopenfilename in vba?
    6 years ago

Post a comment