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

Argument

What It Does

fileFilter

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.

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.

0 0

Responses

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

Post a comment