Microsoft KB Archive/306248

= WD2002: Sample Macro to List All Files in a Folder =

Article ID: 306248

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Word 2002 Standard Edition

-



This article was previously published under Q306248



For a Microsoft Word 2000 version of this article, see 236162.



SUMMARY
This article contains a sample Microsoft Visual Basic for Applications macro that you can use 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 for the listed files
 * The file names of the files found
 * The file sizes of the files found
 * The dates and times 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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to 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 requirements. The following sample macro lists or prints the file name, the file size, and the date and time of all files found in the specified folder: Sub FolderList ' ' Example Macro to list the files contained in a folder. '

Dim x As String, MyName As String Dim i As Integer Dim Response As Integer, TotalFiles As Integer

On Error Resume Next

Folder:

' Prompt the user for the folder to list. x = InputBox(Prompt:=&quot;What folder do you want to list?&quot; & vbCr & vbCr _        & &quot;For example: C:\My Documents&quot;, _         Default:=Options.DefaultFilePath(wdDocumentsPath))

If x = &quot;&quot; Or x = &quot; &quot; Then If MsgBox(&quot;Either you did not type a folder name correctly&quot; _           & vbCr & &quot;or you clicked Cancel. Do you want to quit?&quot; _            & vbCr & vbCr & _            &quot;If you want to type a folder name, click No.&quot; & vbCr & _            &quot;If you want to quit, click Yes.&quot;, vbYesNo) = vbYes Then Exit Sub Else GoTo Folder End If  End If

' Test if folder exists. If Dir(x, vbDirectory) = &quot;&quot; Then MsgBox &quot;The folder does not exist. Please try again.&quot; GoTo Folder End If

' Search the specified folder for files ' and type the listing in the document. With Application.FileSearch .NewSearch .FileType = msoFileTypeOfficeFiles ' Change the .FileType to the type of files you are looking for; ' for example, the following line finds all files: ' .FileType = msoFileTypeAllFiles .LookIn = x     .Execute TotalFiles = .FoundFiles.Count If TotalFiles = 0 Then MsgBox (&quot;There are no files in the folder!&quot; & _              &quot;Please type another folder to list.&quot;) GoTo Folder End If

' Create a new document for the file listing. Application.Documents.Add ActiveDocument.ActiveWindow.View = wdPrintView

' Set tabs. With Selection.ParagraphFormat.TabStops .Add _ Position:=InchesToPoints(3), _ Alignment:=wdAlignTabLeft, _ Leader:=wdTabLeaderSpaces .Add _ Position:=InchesToPoints(4), _ Alignment:=wdAlignTabLeft, _ Leader:=wdTabLeaderSpaces End With

' Type the file list headings. Selection.TypeText &quot;File Listing of the &quot;

With Selection.Font .AllCaps = True .Bold = True End With Selection.TypeText x     With Selection.Font .AllCaps = False .Bold = False End With Selection.TypeText &quot; folder!&quot; & vbLf Selection.Font.Underline = wdUnderlineSingle Selection.TypeText vbLf & &quot;File Name&quot; & vbTab & &quot;File Size&quot; _ & vbTab & &quot;File Date/Time&quot; & vbLf & vbLf Selection.Font.Underline = wdUnderlineNone For i = 1 To TotalFiles MyName = .FoundFiles(i) Selection.TypeText MyName & vbTab & FileLen(MyName) _ & vbTab & FileDateTime(MyName) & vbLf Next i

' Type the total number of files found. Selection.TypeText vbLf & &quot;Total files in folder = &quot; & TotalFiles & _ &quot; files.&quot; End With

If MsgBox(&quot;Do you want to print this folder list?&quot;, vbYesNo)=vbYes Then Application.ActiveDocument.PrintOut End If

If MsgBox(&quot;Do you want to list another folder?&quot;, vbYesNo)=vbYes Then GoTo Folder End If

End Sub

