Sub detectAPassword()
Dim f As Object
Dim filesToProcess As Long
Dim fs As Object
Dim i As Long
Dim myPath As String
Dim thisEntry As String
Application.ScreenUpdating = False
myPath = "C:\test\"
Range("A1:C1").Value = Array("Filename", "Date/Timestamp", "Password protected")
With Application.FileSearch
.NewSearch
.LookIn = myPath
.SearchSubFolders = True
.Filename = "*.xls"
.Execute
filesToProcess = .FoundFiles.Count
'loop through each workbook in the directory
For i = 1 To .FoundFiles.Count
thisEntry = .FoundFiles(i) 'get the next file
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(thisEntry)
ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1).Value = thisEntry
ActiveSheet.Range("B" & Rows.Count).End(xlUp).Offset(1).Value = f.DateLastModified 'put the date in
ActiveSheet.Range("C" & Rows.Count).End(xlUp).Offset(1).Value = False
On Error GoTo errorHandler
Workbooks.Open thisEntry, , , , "12345" 'try to open with this password
ActiveWorkbook.Close 0
doNext:
Next i
End With
Columns("A:C").AutoFit
Application.ScreenUpdating = True
Exit Sub
errorHandler:
'we got an error, so the file has a password
ActiveSheet.Range("C" & Rows.Count).End(xlUp).Value = True
GoTo doNext
End Sub