Sub TestListFilesInFolder()
Dim objdrive As Object
Dim Driveletter As String
Dim boxtitle, Name
Rem Dim Drive_Select As String
'Sets names for all bits of user defined information
Driveletter = Range("c14").Value
'Captures the drive letter selected in the drop down list
answerdrive = MsgBox("You are about to audit " & Driveletter & " Is this correct?", vbYesNo, " Excel Hunter")
If answerdrive = vbNo Then Call HelpSelectDrive
If answerdrive = vbNo Then Exit Sub
'Gives user an "Are you sure" escape clause
'On Error GoTo Err_Trap
'Will exit routine with a message explaining the problem if an invalid drive has been selected from the drop down list e.g. an unmapped one
Workbooks.Add
' create a new workbook for the file list
With Range("A1")
.Formula = "Folder contents:"
.Font.Bold = True
.Font.Size = 12
End With
'Puts header in big font on worksheet
Range("A3").Formula = "File Name:"
Range("B3").Formula = "File Size (Kb):"
Range("C3").Formula = "File Type:"
Range("D3").Formula = "Date Created:"
Range("E3").Formula = "Date Last Accessed:"
Range("F3").Formula = "Date Last Modified:"
Range("G3").Formula = "Author:"
Range("H3").Formula = "Last Modified By:"
Range("H3").Formula = "File Name:"
Range("I3").Formula = "Short File Name:"
Range("A3:I3").Font.Bold = True
'Puts column headers in worksheet
ListFilesInFolder Driveletter, True
MsgBox "Excel Spreadsheets on Drive " & Driveletter, vbOKOnly, "Macro completed"
'Tells user that macro has completed
Exit Sub
Err_Trap:
Application.DisplayAlerts = False
'Turns off automatic "Are you sure" message boxes
ActiveWorkbook.Close
'Closes workbook created with all the headings
MsgBox "Doh! Please select a valid drive letter" & Chr(13) & "e.g. C:\", vbExclamation, " Muppet Instructions"
'Tells the user that they have picked an invalid drive
Application.DisplayAlerts = True
'Turns back on the automatic "Are you sure" messages
End Sub
Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
Dim objdrive As Object
Dim XLSFileName As String
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
Dim DSO As DSOFile.OleDocumentProperties
Set DSO = New DSOFile.OleDocumentProperties
Dim FileItem As Scripting.File
Dim strauthor As BuiltinDocumentProperty
Dim lstAuthor As String
Dim r As Long
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
r = Range("A65536").End(xlUp).Row + 1
For Each FileItem In SourceFolder.Files
'If FileItem.Name = "" Then Exit Sub
XLSFileName = Cells(r, 8).Formula
'DSO.Open sfilename:=FileItem.Name
XLSFileName.ChangeFileAccess xlReadOnly
DSO.Open FileItem.Path
If FileItem.Name = "" Then Exit Sub
On Error Resume Next
If FileItem.Name Like ("*.xls") Then Cells(r, 1).Formula = FileItem.Path
If FileItem.Name Like ("*.xls") Then Cells(r, 2).Formula = FileItem.Size / 1024
If FileItem.Name Like ("*.xls") Then Cells(r, 3).Formula = FileItem.Type
If FileItem.Name Like ("*.xls") Then Cells(r, 4).Formula = FileItem.DateCreated
If FileItem.Name Like ("*.xls") Then Cells(r, 5).Formula = FileItem.DateLastAccessed
If FileItem.Name Like ("*.xls") Then Cells(r, 6).Formula = FileItem.DateLastModified
If FileItem.Name Like ("*.xls") Then Cells(r, 7).Formula = strauthor
If FileItem.Name Like ("*.xls") Then Cells(r, 8).Formula = FileItem.Name
If FileItem.Name Like ("*.xls") Then Cells(r, 9).Formula = FileItem.ShortName
If FileItem.Name Like ("*.xls") Then Cells(r, 10).Value = DSO.SummaryProperties.DateLastSaved
If FileItem.Name Like ("*.xls") Then Cells(r, 11).Formula = DSO.SummaryProperties.ApplicationName
If FileItem.Name Like ("*.xls") Then Cells(r, 12).Value = DSO.SummaryProperties.Author
If FileItem.Name Like ("*.xls") Then Cells(r, 13).Value = DSO.SummaryProperties.DateLastPrinted
If FileItem.Name Like ("*.xls") Then Cells(r, 14).Value = DSO.SummaryProperties.DateLastSaved
If FileItem.Name Like ("*.xls") Then Cells(r, 15).Value = DSO.SummaryProperties.LastSavedBy
If FileItem.Name Like ("*.xls") Then Cells(r, 16).Value = DSO.SummaryProperties.DateCreated
If FileItem.Name Like ("*.xls") Then Cells(r, 17).Value = DSO.SummaryProperties.ByteCount
If FileItem.Name Like ("*.xls") Then r = r + 1
XLSFileName.ChangeFileAccess xlReadWrite
Next FileItem
If IncludeSubfolders Then
For Each SubFolder In SourceFolder.SubFolders
ListFilesInFolder SubFolder.Path, True
Next SubFolder
End If
Columns("A:S").AutoFit
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
ActiveWorkbook.Saved = True
Exit Sub
End Sub