[Public Sub addPassword()
Dim FSO As Object
Dim folder As Object, subfolder As Object
Dim wb As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
folderPath = "C:\Users\Ankush.Saeel\Desktop\New folder"
Set folder = FSO.GetFolder(folderPath)
With Application
.DisplayAlerts = False
.ScreenUpdating = False
.EnableEvents = False
.AskToUpdateLinks = False
End With
For Each wb In folder.Files
If Right(wb.Name, 3) = "xls" Or Right(wb.Name, 4) = "xlsx" Or Right(wb.Name, 4) = "xlsm" Then
'-------------------------------------------------
On Error Resume Next
Workbooks.Open wb, , , , "daafdsfafasfff"
If Err.Number > 0 Then
GoTo 25
End If
'-------------------------------------------------
Set masterWB = Workbooks.Open(wb)
ActiveWorkbook.SaveAs Filename:=Application.ActiveWorkbook.FullName, Password:="123"
ActiveWorkbook.Close True
End If
25 Next '<-------------------------------------------------
For Each subfolder In folder.SubFolders
For Each wb In subfolder.Files
If Right(wb.Name, 3) = "xls" Or Right(wb.Name, 4) = "xlsx" Or Right(wb.Name, 4) = "xlsm" Then
Set masterWB = Workbooks.Open(wb)
ActiveWorkbook.SaveAs Filename:=Application.ActiveWorkbook.FullName, Password:="123"
ActiveWorkbook.Close True
End If
Next
Next
With Application
.DisplayAlerts = True
.ScreenUpdating = True
.EnableEvents = True
.AskToUpdateLinks = True
End With
End Sub]