Visual Basic for Applications Macro That Displays a File List (165643)



The information in this article applies to:

  • Microsoft PowerPoint 97 for Windows
  • Microsoft Word 97 for Windows
  • Microsoft Excel 97 for Windows
  • Microsoft Office 97 for Windows

This article was previously published under Q165643

SUMMARY

This article includes a sample Microsoft Visual Basic for Applications macro that retrieves the file names in a specified directory. The macro displays the file names in a message box.

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 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.

NOTE: The maximum number of characters that a message box prompt can contain is 1,024 characters, depending on the width of the characters that are used. If you use more than the maximum, the results of your directory listing may be truncated.

Sample Macro

Sub GetFileList()

   Dim oSearch As FileSearch

   ' Change strDirName to the path of the directory for which you want a
   ' listing.
   Dim strDirName As String:  strDirName = CurDir()
   Dim strPrompt As String: strPrompt = strDirName & Chr(13) & Chr(13)
   Dim i As Long

   ' Get a reference to the FileSearch object.
   Set oSearch = Application.FileSearch

   With oSearch

      ' Reset the search.
      .NewSearch

      ' Specify the directory that contains the files you want to count.
      ' The variable strDirName is the parameter of the CountAllFiles()
      ' function.
      .LookIn = strDirName

      ' Exclude subfolders.
      .SearchSubFolders = False

      ' Specify what types of files you want to find.
      ' For example, to search for text files change this line to:
      '   .FileName "*.txt"
      .FileName = "*.*"

      ' The Execute command begins the search.
      .Execute

      ' Check to see if any files were found.
      If .FoundFiles.Count > 0 Then

         ' Loop through the files that were found and store the file names
         ' in the variable strPrompt.
         For i = 1 To .FoundFiles.Count
            strPrompt = strPrompt & .FoundFiles(i) & Chr(13)
         Next i
      Else
         strPrompt = strPrompt & "No files found."
      End If

      ' Display the file list in a message box.
      MsgBox strPrompt

   End With

End Sub
				

Modification Type:MinorLast Reviewed:9/13/2006
Keywords:kbhowto kbprogramming KB165643