You are currently offline, waiting for your internet to reconnect

XL97: How to Use the GetOpenFilename Method

This article was previously published under Q161930
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
This article provide instructions and examples on using theGetOpenFilename method in a Visual Basic for Applications macro.
Microsoft 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 -

Microsoft Advisory Services -

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:;EN-US;CNTACTMSThe GetOpenFilename method in Visual Basic for Applications allows you todisplay the Open dialog in Microsoft Excel and get a file name from a userwithout actually opening any files. Normally the file name is returned toa variable and used later in the macro. This method has five arguments, allof which are optional:
Providing no arguments to the function allows the Open dialog to bedisplayed using the All Files (*.*) file filter and uses the defaultdialog box title. Here is an overview of each of the five arguments:


This argument has two parts. The first part is the text that will appearin the List Files of Type dropdown box of the Open dialog. The secondpart of the argument determines what files are actually shown. Thefollowing 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 separatewildcard expressions. This example filters on all files ending in TXT andBAS:
   X = Application.GetOpenFilename _				
("Visual Basic Files (*.txt; *.bas), *.txt, *.bas")

When using the FileFilter argument, the value you specify is the only onethat appears on the List Files of Type: dropdown box. You can list otheritems in the dropdown list as well. This example lists two types of filesin the dropdown box with the first one being the default selection:
   X = Application.GetOpenFilename _				
("Text Files (*.txt), *.txt, Add-in Files (*.xla), *.xla")


This optional argument specifies which file filter to use by default. Ifno filter index is specified, or the filter index is greater than thenumber of filters specified, the first filter is used. This example usestwo file filters but selects the second one (*.xla file) by default:
   X = Application.GetOpenFilename _				
("Text Files (*.txt), *.txt, Add-in Files (*.xla), *.xla", 2)


The title specifies the text that will appear at the top of the displayeddialog box. The text Open My Files will appear on the dialog using thisexample:
   X = Application.GetOpenFilename _				
("Text Files (*.txt), *.txt", 1, "Open My Files")


This 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 filefrom the open box. It can be set to True or False. If True, the variablemust be defined as a variant data type, as the return value will alwaysbe an array, even if only one file is selected. This example willincorporate all of the arguments above and loop through all selected filesand open them:
   Sub Open_Files       'Defines the variable as a variant data type       Dim X as variant       'Opens the dialog       X = Application.GetOpenFilename _           ("Text Files (*.txt), *.txt, Add-in Files (*.xla), *.xla", 2, _           "Open My Files", ,True)       'Loops through every file that is selected and opens each one       For Y = 1 to Ubound(X)           Workbooks.Open X(Y)       Next   End Sub				
One problem you may encounter is that the macro produces a Type mismatcherror if the user clicks the cancel button from the dialog box. The valueof the variable will be set to False. Standard error-trapping techniquescan be used to trap this problem:
Sub Open_Files       'Defines the variable as a variant data type       Dim X as variant       'Continues to run the macro even if an error occurs       On Error Resume Next       'Opens the dialog       X = Application.GetOpenFilename _           ("Text Files (*.txt), *.txt, Add-in Files (*.xla), *.xla", 2, _           "Open My Files", ,True)       'Tests the variable X to see if it is valid       If X = False then GoTo Cancel       'Loops through every file that is selected and opens each one       For Y = 1 to Ubound(X)           Workbooks.Open X(Y)       Next       Exit Sub       'If X was equal to false, displays a message and exits the macro   Cancel:       Msgbox "The Cancel button was selected."   End Sub				
For more information on using the GetOpenFilename method, query on thefollowing article(s) in the Microsoft Knowledge Base:
153722 XL: GetOpenFilename Method Is Different in MS Excel for Win 95

141574 XL: How to Create Macro That Opens Multiple Selected Files
XL97 xlDialogOpen

Article ID: 161930 - Last Review: 11/23/2006 07:56:06 - Revision: 2.4

  • Microsoft Excel 97 Standard Edition
  • KB161930