Try this version...
Option Explicit
Sub PW_Change_v3()
'<<< Not Tested - Run on a copy of your files >>>>
Dim AllFolders As Object, AllFiles As Object
Dim MyPath As String, MyFolderName As String, MyFileName As String
Dim i As Integer, k As Variant
Dim OldPW As String, NewPW As String, s As String
'Change these as needed
MyPath = "C:\Temp\"
OldPW = "Old Password"
NewPW = "New Password"
Set AllFolders = CreateObject("Scripting.Dictionary")
Set AllFiles = CreateObject("Scripting.Dictionary")
'Get all folders and any subfolders
AllFolders.Add (MyPath), ""
i = 0
Do While i < AllFolders.Count
k = AllFolders.keys
MyFolderName = Dir(k(i), vbDirectory)
Do While MyFolderName <> ""
If MyFolderName <> "." And MyFolderName <> ".." Then
If (GetAttr(k(i) & MyFolderName) And vbDirectory) = vbDirectory Then
AllFolders.Add (k(i) & MyFolderName & "\"), ""
End If
End If
MyFolderName = Dir
Loop
i = i + 1
Loop
'Get all files in all folders and subfolders
For Each k In AllFolders.keys
MyFileName = Dir(k & "*.xls*")
Do While MyFileName <> ""
AllFiles.Add (k & MyFileName), ""
MyFileName = Dir
Loop
Next k
'Change passwords
With Application
.ScreenUpdating = False
.DisplayAlerts = False
On Error Resume Next
For Each k In AllFiles.keys
Workbooks.Open Filename:=k, writeresPassword:=OldPW, ignorereadonlyrecommended:=True
If Err = 1004 Then
s = s & vbNewLine & k
Err.Clear
GoTo Skip
End If
ActiveWorkbook.SaveAs k, writeresPassword:=NewPW, ReadOnlyRecommended:=True
ActiveWorkbook.Close
Skip:
Next k
On Error GoTo 0
.DisplayAlerts = True
.ScreenUpdating = True
End With
MsgBox "These files were not changed - incorrect password. " & s, vbInformation, "Incorrect Password"
Set AllFolders = Nothing
Set AllFiles = Nothing
End Sub
Bookmarks