Word

Create a Directory File Listing

Ease of Use

Intermediate

Version tested with

2000, 2002 

Submitted by:

lucas

Description:

Browse to a directory and list all documents and file size in MB in a new Word Document 

Discussion:

Your sending a lot of files to clients on a regular basis and wish to document which files are sent and when. This code allows you to select a directory and create a new document which lists all files in the directory. Sets a header which tells their file path on your computer (line can be commented out if you don't wish to use it) and a heading of your choice. It also numbers the files and lists their size in MB. Presently set up to look for files with 3 letter file extentions but can be changed in the code(commented). 

Code:

instructions for use

			

Put this code In a standard module: Option Explicit Sub runform() UserForm1.Show End Sub This Is the code For your form: Option Explicit Private Sub UserForm_Initialize() PopListBox End Sub Private Sub PopListBox() Dim oFileSysObj As Object Dim oFileSearch As Object Dim oDrive As Object Dim oFolder As Object Dim oSubFolder As Object Dim sFileName As String Dim i As Integer lstFolders.Clear 'clear list Set oFileSysObj = CreateObject("Scripting.FileSystemObject") If Not oFileSysObj.FolderExists(txtPath.Text) Then 'if no such folder txtPath.Text = "" For Each oDrive In oFileSysObj.Drives 'list drives lstFolders.AddItem oDrive.DriveLetter & ":" Next Else 'if folder does exist Set oFolder = oFileSysObj.GetFolder(txtPath.Text) For Each oSubFolder In oFolder.subfolders lstFolders.AddItem oSubFolder.Name 'add each subfolder Next Set oFileSearch = Application.FileSearch With oFileSearch .LookIn = txtPath.Text .FileName = "*.*" If .Execute > 0 Then For i = 1 To .FoundFiles.Count sFileName = oFileSysObj.getfilename(.FoundFiles(i)) lstFolders.AddItem sFileName 'add each file Next i End If End With End If Set oFileSysObj = Nothing Set oFileSearch = Nothing Set oDrive = Nothing Set oFolder = Nothing Set oSubFolder = Nothing End Sub Private Sub cmdExport_Click() Dim i As Integer, n As Integer 'Code for the export list button ' Create a new document for the file listing. Application.Documents.Add ' Set tabs. With Selection.ParagraphFormat.TabStops .Add _ Position:=CentimetersToPoints(1), _ Alignment:=wdAlignTabLeft, _ Leader:=wdTabLeaderSpaces End With With Selection.ParagraphFormat.TabStops .Add _ Position:=CentimetersToPoints(14), _ Alignment:=wdAlignTabRight, _ Leader:=wdTabLeaderSpaces End With 'Configure the appearance of your headers here 'you can add extra & vbLf to the end of lines for additional paragraph returns WriteLine "File listing of " & txtPath.Text & vbLf & vbLf, True ' WriteLine "File listing of " & txtPath.Text & vbLf, True WriteLine "File Name" & vbTab & "File Size" & vbLf, True n = 1 For i = 0 To lstFolders.ListCount - 1 'will only return files with 3 letter file extentions 'to look for html files or files with 2 letter extentions 'change the number 3 at the end of the next line. If Len(lstFolders.List(i)) > 4 And InStr(lstFolders.List(i), ".") = Len(lstFolders.List(i)) - 3 Then 'check if the name is "*.???" WriteLine n & vbTab & lstFolders.List(i) & vbTab & _ Format(((FileLen(txtPath.Text & lstFolders.List(i)) / 1024) / 1024), "#.0") & "Mb" & vbLf, False n = n + 1 End If Next i End Sub Sub WriteLine(outputline As String, IsBold As Boolean) 'Formatting for your WriteLine or Headers Selection.Font.Bold = IsBold Selection.Font.Name = "Arial" Selection.Font.Size = 10 Selection.TypeText outputline End Sub Private Sub lstFolders_DblClick(ByVal Cancel As MSForms.ReturnBoolean) 'Returns a folder listing and adds it to the listbox txtPath.Text = txtPath.Text & lstFolders.Text & "\" PopListBox End Sub Private Sub cmdUpLevel_Click() 'Code for the up a directory level button Dim a As Integer a = InStrRev(txtPath.Text, "\", Len(txtPath.Text) - 1, 1) If a = 0 Then txtPath.Text = "" Else txtPath.Text = Left(txtPath.Text, a) End If PopListBox End Sub Private Sub txtPath_Exit(ByVal Cancel As MSForms.ReturnBoolean) PopListBox End Sub Private Sub cmdClose_Click() 'Code for the close button on the form Unload Me End Sub

How to use:

  1. Open the Visual Basic Editor by going to tools-Macro's-Visual Basic Editor or use Alt-F11
  2. On the toolbar of the Visual Basic Editor, go to insert - module
  3. In the module pane paste the code above for the standard module.
  4. On the toolbar of the Visual Basic Editor, go to insert - Userform
  5. From the Controls Toolbox add the following to your userform:
  6. 3 buttons named cmdUpLevel, cmdExport, and cmdClose
  7. 1 textbox named txtPath
  8. 1 listbox named lstFolders
  9. (you can change the names above but you must also change them in the code if you do)
  10. Right click on Userform1 in the project explorer and left click on view code
  11. Paste the code for the userform from above into the module
  12. Close the Visual Basic Editor by clicking the X in the upper right corner or go to File-Close
 

Test the code:

  1. Click on Tools-Macro-Macros and select the Runform macro from the list
  2. Click on run
  3. Select the directory you wish to create a listing for and click Export List
 

Sample File:

FileList.zip 18.68KB 

Approved by mdmackillop


This entry has been viewed 197 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express