XL: GetOpenFilename Method Is Different

ID: Q153722

The information in this article applies to:

SYMPTOMS

The Visual Basic for Applications GetOpenFilename method works differently in Microsoft Excel for Windows 95 and later than in Microsoft Excel version 5.0. Also, the Help file entry on GetOpenFilename in Microsoft Excel for Windows 95, version 7.0 and Microsoft Excel 97, is not complete on how the FileFilter argument affects the method.

CAUSE

The FileFilter argument for the GetOpenFilename method contains two parts. In Microsoft Excel, version 5.0, the first part, which is before the comma, appears in the "List Files of Type:" field in the File Open dialog box. The second filter, which is after the comma, appears in the File Name field of the dialog box and is the part that actually affects what files are shown. If you leave out the second part, the function shows all files. The first part has no effect on what files are shown. It can be anything. Therefore, you must make sure the two parts match, because you could have a filter in the "List Files of Type:" field that does not match the list of files shown.

In Microsoft Excel for Windows 95 and Microsoft Excel 97, the Filefilter argument affects the GetOpenFilename differently. The entire Filefilter string appears in the Files Of Type field. The first part has the effect of determining what files will be listed. The first part will do this even if the second part is different. The second part of the Filefilter argument (the part after the comma) does not determine the list of files displayed unless it is used by itself. The first part takes precedence.

Additionally, characters before a wildcard ("*") are fully recognized in Microsoft Excel, version 5.0. In Microsoft Excel for Windows 95 and Microsoft Excel 97, characters before a wildcard are only recognized in the file extension, not in the file name. Any filter with characters before a wildcard in the file name is ignored. This means something like "Test*.*" does not work as a Filefilter in Microsoft Excel for Windows 95 and Microsoft Excel 97. It shows no file filter in the open dialog box and lists all files in the current folder (directory).

WORKAROUND

For simplification, in Microsoft Excel for Windows 95 and Microsoft Excel 97, you can use the first filter as if it were the only filter. However, you must include the comma to avoid a syntax error. Also, you should avoid characters before a wildcard in the file name.

For example, use the following format in the module code:

     fileFilter:="Text Files (*.txt),"

MORE INFORMATION

The examples below depict the differences between the two versions when running variations of GetOpenFilename. Each example contains a macro that can either be run in Microsoft Excel 5.0, Microsoft Excel for Windows 95, or in Microsoft Excel 97. The comments before each macro describe the results you receive in each of the three versions.

Visual Basic Code Examples

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 professionals 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. To illustrate, the following are a few examples of the various differences between the two versions.

Example 1

Microsoft Excel version 5.0 shows "Text Files (test*.*)" in the "List Files of Type" field and only lists files beginning with "test."

Difference: Microsoft Excel for Windows 95 and Microsoft Excel 97 show nothing in the "Files of Type" field and lists all files.

   Sub Example1()
      x = Application.GetOpenFilename _
         (fileFilter:="Text Files (test*.*),test*.*")
   End Sub

Example 2

Microsoft Excel version 5.0 shows "Text Files (*.txt)" in the "List Files of Type:" field and only lists files ending in "xls".

Difference: Microsoft Excel for Windows 95 and Microsoft Excel 97 show "Text Files (*.txt)" in the "Files of Type" field and displays files ending in ".txt".

   Sub Example2()
       x = Application.GetOpenFilename _
          (fileFilter:="Text Files (*.txt),*.xls")
   End Sub

Example 3

Microsoft Excel version 5.0 shows "Text Files (*.txt)" in the "List Files of Type:" field and lists all files.

Difference: Microsoft Excel for Windows 95 and Microsoft Excel 97 show "Text Files (*.txt)" in the "Files of Type" field and displays files ending in ".txt".

   Sub Example3()
      x = Application.GetOpenFilename _
         (fileFilter:="Text Files (*.txt),")
   End Sub

Example 4

Microsoft Excel version 5.0 shows "Text Files (bob*.txt)" in the "List Files of Type:" field and displays files beginning with "bob" and ending with "txt".

Difference: Microsoft Excel for Windows 95 and Microsoft Excel 97 show nothing in the "Files of Type" field and lists all files.

   Sub Example4()
      x = Application.GetOpenFilename _
         (fileFilter:="Text Files (bob*.txt),bob*.txt")
   End Sub

Example 5

Microsoft Excel version 5.0 shows "Text Files (*.tx*)" in the "List Files of Type:" field and displays files with ".tx" as part of the extension.

Difference: Microsoft Excel for Windows 95 and Microsoft Excel 97 show "Text Files (*.tx*)" in the "Files of Type" field and displays files with ".tx" as part of the extension.

   Sub Example5()
      x = Application.GetOpenFilename _
         (fileFilter:="Text Files (*.tx*),*.tx*")
   End Sub

Example 6

Microsoft Excel version 5.0 shows nothing in the "List Files of Type:" field and displays files ending in ".txt".

Difference: Microsoft Excel for Windows 95 and Microsoft Excel 97 show "(*.txt)" in the "Files of Type" field and displays files ending in ".txt".

   Sub Example6()
      x = Application.GetOpenFilename(",*.txt")
   End Sub

REFERENCES

Microsoft Excel 97

For more information about GetOpenFilename, click the Office Assistant, type "GetOpenFilename," click Search, and then click to view "GetOpenFilename Method."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Excel Visual Basic for Applications Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q120802
   TITLE     : Office: How to Add/Remove a Single Office
               Program or Component

Microsoft Excel for Windows 95

For more information about GetOpenFilename, in Microsoft Excel for Windows 95, click Answer Wizard on the Help menu and type:

   GetOpenFilename

Additional query words: 5.00 5.00c 7.00 7.00a XL97 8.00
Keywords          :  
Version           : 5.00 5.00c 7.00 7.00a
Platform          : WINDOWS

Last Reviewed: May 18, 1999