Article ID: 161930 - Last Review: November 23, 2006 - Revision: 2.4 XL97: How to Use the GetOpenFilename MethodThis article was previously published under Q161930 On This PageSUMMARY
This article provide instructions and examples on using the
GetOpenFilename method in a Visual Basic for Applications macro.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites: Microsoft Certified Partners - https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104) Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (http://support.microsoft.com/gp/advisoryservice) For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms) The GetOpenFilename method in Visual Basic for Applications allows you to display the Open dialog in Microsoft Excel and get a file name from a user without actually opening any files. Normally the file name is returned to a variable and used later in the macro. This method has five arguments, all of which are optional:
FileFilter
Providing no arguments to the function allows the Open dialog to be
displayed using the All Files (*.*) file filter and uses the default
dialog box title. Here is an overview of each of the five arguments:
FilterIndex Title ButtonText MultiSelect FileFilterThis argument has two parts. The first part is the text that will appear in the List Files of Type dropdown box of the Open dialog. The second part of the argument determines what files are actually shown. The following example will show all text files in the current directory:
X = Application.GetOpenFilename("Text Files (*.txt), *.txt")
You may also use multiple wildcard expressions to filter on two separate wildcard expressions. This example filters on all files ending in TXT and BAS: X = Application.GetOpenFilename _ When using the FileFilter argument, the value you specify is the only one that appears on the List Files of Type: dropdown box. You can list other items in the dropdown list as well. This example lists two types of files in the dropdown box with the first one being the default selection: X = Application.GetOpenFilename _ FilterIndexThis optional argument specifies which file filter to use by default. If no filter index is specified, or the filter index is greater than the number of filters specified, the first filter is used. This example uses two file filters but selects the second one (*.xla file) by default:X = Application.GetOpenFilename _ TitleThe title specifies the text that will appear at the top of the displayed dialog box. The text Open My Files will appear on the dialog using this example:X = Application.GetOpenFilename _ ButtonTextThis argument is used only on Macintosh computers and may be ignored, although you must still allocate space for it in your arguments. This argument specifies whether the user may select more than one file from the open box. It can be set to True or False. If True, the variable must be defined as a variant data type, as the return value will always be an array, even if only one file is selected. This example will incorporate all of the arguments above and loop through all selected files and open them: | Article Translations
|

Back to the top
