B
Boss
Hi,
I am using the below code to password protect all the files in a folder and
its subfodlers.
The code opens all the files one by one and saves them with a password. If
any file is password protected macro gives me an error.
please help me solve this... I tried with a error handler which will move to
next file on a error but didnt worked properly.
Sub ExecuteListFiles()
Range("A20").Select
Application.ScreenUpdating = False
Application.StatusBar = "Processing... "
Dim strpathfile As String
strpathfile = Range("c6").Value 'sets path
Call ListFilesInFolder(strpathfile, True)
Application.ScreenUpdating = True
Application.StatusBar = ""
Range("A1").Select
MsgBox ("Done with all files"), vbExclamation
End Sub
Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As
Boolean)
Dim FSO As New FileSystemObject, SourceFolder As Folder, Subfolder As
Folder, FileItem As File
Dim lngCount As Long, strSQL As String
Dim pptfile As Object
Dim ws As Worksheet
Set SourceFolder = FSO.GetFolder(SourceFolderName)
For Each FileItem In SourceFolder.Files
ActiveCell.Value = FileItem
ActiveCell.Offset(1, 0).Select
Select Case Right(FileItem.Name, 3) ' finds extension of file
'******************************** Excel ************************************
Case "xls" ' finds excel file
Application.DisplayAlerts = False
Workbooks.Open (FileItem), Password:=""
For Each ws In ActiveWorkbook.Worksheets
With ws.PageSetup
.LeftFooter = "&BData Classification : Highly Confidential&B"
End With
Next ws
' password for excel
ActiveWorkbook.SaveAs FileName:=FileItem, FileFormat:=xlNormal,
Password:="test", _
WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.Close savechanges:=True
Application.DisplayAlerts = True
End Select
' loop in folders and sub folders
lngCount = lngCount + 1
Next FileItem
If IncludeSubfolders Then
For Each Subfolder In SourceFolder.SubFolders
ListFilesInFolder Subfolder.path, True
Next Subfolder
End If
Set FileItem = Nothing
Set SourceFolder = Nothing
End Sub
Thx!
I am using the below code to password protect all the files in a folder and
its subfodlers.
The code opens all the files one by one and saves them with a password. If
any file is password protected macro gives me an error.
please help me solve this... I tried with a error handler which will move to
next file on a error but didnt worked properly.
Sub ExecuteListFiles()
Range("A20").Select
Application.ScreenUpdating = False
Application.StatusBar = "Processing... "
Dim strpathfile As String
strpathfile = Range("c6").Value 'sets path
Call ListFilesInFolder(strpathfile, True)
Application.ScreenUpdating = True
Application.StatusBar = ""
Range("A1").Select
MsgBox ("Done with all files"), vbExclamation
End Sub
Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As
Boolean)
Dim FSO As New FileSystemObject, SourceFolder As Folder, Subfolder As
Folder, FileItem As File
Dim lngCount As Long, strSQL As String
Dim pptfile As Object
Dim ws As Worksheet
Set SourceFolder = FSO.GetFolder(SourceFolderName)
For Each FileItem In SourceFolder.Files
ActiveCell.Value = FileItem
ActiveCell.Offset(1, 0).Select
Select Case Right(FileItem.Name, 3) ' finds extension of file
'******************************** Excel ************************************
Case "xls" ' finds excel file
Application.DisplayAlerts = False
Workbooks.Open (FileItem), Password:=""
For Each ws In ActiveWorkbook.Worksheets
With ws.PageSetup
.LeftFooter = "&BData Classification : Highly Confidential&B"
End With
Next ws
' password for excel
ActiveWorkbook.SaveAs FileName:=FileItem, FileFormat:=xlNormal,
Password:="test", _
WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.Close savechanges:=True
Application.DisplayAlerts = True
End Select
' loop in folders and sub folders
lngCount = lngCount + 1
Next FileItem
If IncludeSubfolders Then
For Each Subfolder In SourceFolder.SubFolders
ListFilesInFolder Subfolder.path, True
Next Subfolder
End If
Set FileItem = Nothing
Set SourceFolder = Nothing
End Sub
Thx!