WD2000: Sample Macro to List All Files in a Folder

ID: Q236162


The information in this article applies to:
  • Microsoft Word 2000


SUMMARY

This article contains a sample Microsoft Visual Basic for Applications macro that can be used to create a list of all files in a specified folder. The macro returns the following information to a new Word document:

  • The folder name of the listed files.
  • The file name of the files found.
  • The file size of the files found.
  • The date and time of the files found.
  • The total number of files listed.
The macro creates a list of files similar to the following example:

   File Listing of the C:\MY DOCUMENTS folder!

   File Name                       File Size       File Date/Time
   ----------------------------------------------------------------------

   Background.doc                  1461            2/24/99 2:16:52 PM
   Backup of Background.wbk        1461            2/24/99 2:14:10 PM
   Book1.xls                       15360           6/15/99 3:07:42 PM
   Book2.xls                       13824           11/12/98 4:28:50 PM
   Book3.xls                       13824           11/24/98 9:38:32 AM

   Total files in folder = 5 files. 


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 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 Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
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/overview/overview.asp
The following sample macro lists or prints the file name, the file size, and the date/time of all files found in the specified folder:

Sub FolderList()
'
' Example Macro to list the files contained in a folder.
'

Dim x, fs
Dim I As Integer
Dim y As Integer
Dim Folder, MyName, TotalFiles, Response, PrintResponse, AgainResponse

On Error Resume Next

Folder:
' Prompt the user for the folder to list.
x = InputBox("What folder do you want to list?" & Chr$(13) & Chr$(13) _
   & "For example: C:\My Documents")

If x = "" Or x = " " Then
    Response = MsgBox("Either you did not type a folder name correctly" _
    & Chr$(13) & "or you clicked Cancel. Do you want to quit?" _
    & Chr$(13) & Chr$(13) & _
    "If you want to type a folder name, click No." & Chr$(13) & _
    "If you want to quit, click Yes.", vbYesNo)

        If Response = "6" Then
            End
        Else
            GoTo Folder
        End If
Else

' Test if folder exists.
Set Folder = CreateObject("Scripting.filesystemobject")
If Folder.folderexists(x) = "True" Then

' Search the specified folder for files and type the listing in the ' document.
    
    With Application.FileSearch
        Set fs = Application.FileSearch
        fs.NewSearch
            With fs.PropertyTests
                .Add Name:="Files of Type", _
                Condition:=msoConditionFileTypeAllFiles, _
                Connector:=msoConnectorOr
            End With
        .LookIn = x
        .Execute
        TotalFiles = .FoundFiles.Count
        
                If TotalFiles <> 0 Then
                
                    ' Create a new document for the file listing.
                    Application.Documents.Add
                    ActiveDocument.ActiveWindow.View = wdPrintView
                    
                    ' Set tabs.
                    Selection.WholeStory
                    Selection.ParagraphFormat.TabStops.ClearAll
                    ActiveDocument.DefaultTabStop = InchesToPoints(0.5)
                    Selection.ParagraphFormat.TabStops.Add _
                       Position:=InchesToPoints(3), _
                       Alignment:=wdAlignTabLeft, _
                       Leader:=wdTabLeaderSpaces
                    Selection.ParagraphFormat.TabStops.Add _
                       Position:=InchesToPoints(4), _
                       Alignment:=wdAlignTabLeft, _
                       Leader:=wdTabLeaderSpaces
                    
                    ' Type the file list headings.
                    Selection.TypeText "File Listing of the "
                    
                    With Selection.Font
                        .AllCaps = True
                        .Bold = True
                    End With
                    Selection.TypeText x
                    With Selection.Font
                        .AllCaps = False
                        .Bold = False
                    End With
                    
                    Selection.TypeText " folder!" & Chr$(13)
                    
                    With Selection.Font
                        .Underline = wdUnderlineSingle
                    End With
                    With Selection
                        .TypeText Chr$(13)
                        .TypeText "File Name" & vbTab & "File Size" _
                           & vbTab & "File Date/Time" & Chr$(13)
                        .TypeText Chr$(13)
                    End With
                        With Selection.Font
                        .Underline = wdUnderlineNone
                    End With
        
                Else
                    MsgBox ("There are no files in the folder!" & _
                       "Please type another folder to list.")
                    GoTo Folder
                End If
        
        For I = 1 To TotalFiles
            MyName = .FoundFiles.Item(I)
            .FileName = MyName
            Selection.TypeText .FileName & vbTab & FileLen(MyName) _
               & vbTab & FileDateTime(MyName) & Chr$(13)
        Next I
        
        ' Type the total number of files found.
        Selection.TypeText Chr$(13)
        Selection.TypeText "Total files in folder = " & TotalFiles & _
           " files."
    
    End With
    
    Else
        MsgBox "The folder does not exist. Please try again."
        GoTo Folder
    End If
End If

PrintResponse = MsgBox("Do you want to print this folder list?", vbYesNo)
    If PrintResponse = "6" Then
        Application.ActiveDocument.PrintOut
    End If
    
AgainResponse = MsgBox("Do you want to list another folder?", vbYesNo)
    If AgainResponse = "6" Then
        GoTo Folder
    Else
        End
    End If

End:
End Sub 


REFERENCES

For more information about using the sample code in this article, please see the following article in the Microsoft Knowledge Base:

Q212536 OFF2000: How to Run Sample Code from Knowledge Base Articles

For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:
Q226118 OFF2000: Programming Resources for Visual Basic for Applications

Additional query words: vba

Keywords : kbdta kbmacroexample kbwordvba wd2000
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: August 26, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.