XL97: How to Use FileSearch to Fill a Listbox with File Names

Last reviewed: March 5, 1998
Article ID: Q155837
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

In Microsoft Excel 97 for Windows, you can use the FileSearch object to locate files on a Local or Network drive. This article provides an example of how to use the FileSearch object in Visual Basic for Applications to fill a list box with file names.

MORE INFORMATION

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 engineers 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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

To run the macro, follow these steps:

  1. On the the Tools menu, click Macro, and then click Visual Basic Editor.

  2. On the Insert menu, click UserForm to create a new UserForm.

  3. Select the ListBox control in the Toolbox to add a Listbox to the UserForm.

  4. On the Insert menu, click Module to insert a new module sheet.

  5. In the new module sheet, type the following macro code:

          Sub FileSearchListBox()
    
             ' Dimension variables.
             Dim myarray()
             Dim fs As Object
             Dim i As Integer
    
             ' Declare filesearch object.
             Set fs = Application.FileSearch
    
             ' Set folder to search.
             fs.LookIn = "c:\xldocs"
    
             ' Set file name to search for
             fs.FileName = "*.xls"
    
             ' Execute the file search, and check to see if the file(s) are 
             ' present.
             If fs.execute > 0 Then
    
                ' Redimension the array to the number of files found.
                ReDim myarray(fs.FoundFiles.Count)
    
                ' Loop through all found file names and fill the array.
                For i = 1 To fs.FoundFiles.Count
                   myarray(i) = fs.FoundFiles(i)
                Next i
             Else
                ' Display message if no files were found.
                MsgBox "No files were found"
             End If
    
             ' Loop through the array and fill the list box on the UserForm.
             For i = 1 To fs.FoundFiles.Count
                UserForm1.ListBox1.AddItem myarray(i)
             Next i
    
             ' Display the UserForm.
             UserForm1.Show
    
          End Sub
    
    

REFERENCES

For more information about the FileSearch property, click the Index tab in Visual Basic Help, type the following text

   FileSearch

and then double-click the selected text to go to the "FileSearch property" topic.


Additional query words: 97 file search directory file list
Keywords : kbcode kbprg
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto


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: March 5, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.