XL97: How to Use the GetOpenFilename Method

Last reviewed: September 3, 1997
Article ID: Q161930
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

This article provide instructions and examples on using the GetOpenFilename method in a Visual Basic for Applications macro.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support Engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

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
   FilterIndex
   Title
   ButtonText
   MultiSelect

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:

FileFilter

This 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 _
       ("Visual Basic Files (*.txt; *.bas), *.txt, *.bas")

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 _
       ("Text Files (*.txt), *.txt, Add-in Files (*.xla), *.xla")

FilterIndex

This 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 _
       ("Text Files (*.txt), *.txt, Add-in Files (*.xla), *.xla", 2)

Title

The 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 _
       ("Text Files (*.txt), *.txt", 1, "Open My Files")

ButtonText

This argument is used only on Macintosh computers and may be ignored, although you must still allocate space for it in your arguments.

MultiSelect

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:

   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 mismatch error if the user clicks the cancel button from the dialog box. The value of the variable will be set to False. Standard error-trapping techniques can 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 the following article(s) in the Microsoft Knowledge Base:

   ARTICLE-ID: Q153722
   TITLE     : XL: GetOpenFilename Method Is Different in MS Excel
               for Win 95

   ARTICLE-ID: Q141574
   TITLE     : XL: How to Create Macro That Opens Multiple Selected Files


Additional query words: XL97 xlDialogOpen
Keywords : PgmOthr xlvbahowto xlvbainfo kbcode kbusage
Version : 97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 3, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.