PDA

View Full Version : [SOLVED] UnProtect al files in a folder



Tom Jones
02-18-2014, 07:31 AM
Hello,

In a folder I have several files with passwords. All have the same password. How can I unprotect them to work in them and then protect them in place all at once.
If it can simultaneously deprotected and sheets as well. All sheets have the same password but different from the password for opening the file.
Thanks in advance.

p45cal
02-18-2014, 01:53 PM
This should helpyou along your way
Sub blah()
Set objFSO = CreateObject("Scripting.FileSystemObject")
mFolder = "C:\Documents and Settings\ANOther\My Documents\" 'adjust this
Set mainfolder = objFSO.GetFolder(mFolder)
For Each fil In mainfolder.Files
xx = Split(fil.Name, ".")
If LCase(Left(xx(UBound(xx)), 3)) = "xls" Then
Stop
Set myWb = Workbooks.Open(fil)
myWb.Unprotect "password_to_unprotect_workbook" 'adjust this
'code here to work on workbook as a whole
For Each sht In myWb.Sheets
'allow vba code to work on protected sheet:
sht.Protect Password:="password_to_unprotect_sheet", userinterfaceonly:=True ' adjust this but no need to reprotect as userinterfaceonly dies on file closing.
'code here to work on sheet
Next sht
myWb.Protect Password:="password_to_unprotect_workbook", structure:=True
myWb.Close True
End If
Next fil
End Sub

Tom Jones
02-18-2014, 03:12 PM
Excellent. Thank you very much p45cal.