I think it's a combination of:
1) Suppress display messages
2) Pass a fake password to every wb opened. If no password protection exists, no problem, if password protection exists, you will get an error if the password is wrong. This is good.
3) Trap the error and "skip" all the other action code that won't work, closing the wb and moving on to the next wb
Like so:
Option Explicit
Sub Test()
Dim wb As Workbook
Dim fname As String
Dim fpath As String
Application.DisplayAlerts = False
On Error GoTo ErrorSkip
fpath = "C:\Temp\" 'remember the final \ in this string
fname = Dir("C:\Temp\" & "*.xls")
Do While Len(fname) > 0
Set wb = Workbooks.Open(fpath & fname)
wb.Unprotect "cat"
MsgBox "No password!"
'other action code here
ErrorSkip:
wb.Close False
fname = Dir
Loop
End Sub
I imagine you could also collect the names of the failed files to display later for reference. Something like:
Option Explicit
Sub Test()
Dim wb As Workbook
Dim fname As String
Dim fpath As String
Dim Oops As String
Application.DisplayAlerts = False
On Error GoTo ErrorSkip
fpath = "C:\Temp\" 'remember the final \ in this string
fname = Dir("C:\Temp\" & "*.xls")
Do While Len(fname) > 0
Set wb = Workbooks.Open(fpath & fname)
wb.Unprotect "cat"
'other action code here
ErrorResume:
wb.Close False
fname = Dir
Loop
If Len(Oops) > 0 Then MsgBox "The following files were protected:" & vbLf & Oops
Exit Sub
ErrorSkip:
Oops = Oops & " " & fname
GoTo ErrorResume
End Sub
Bookmarks