Remove known passwords from multiple files in folder

ilsley_excel

Board Regular
Joined
Mar 5, 2015
Messages
54
Office Version
  1. 2010
Platform
  1. Windows
Hi All! I have a folder with several .xlsx files which I wanted to password protect with the same password, which I did using the following code:

Code:
Public Sub LockSpreadsheets()


  Dim FSO As Object
  Dim Folder As Object, subfolder As Object
  Dim wb As Object


  Set FSO = CreateObject("Scripting.FileSystemObject")
  'update the path where the files are saved below
  folderPath = "R:\Data Collection\Tutor Reports\2017-18\Y7 Y8 Y9 Y10\"
  Set Folder = FSO.GetFolder(folderPath)


     With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
    .EnableEvents = False
    .AskToUpdateLinks = False


  End With


  For Each wb In Folder.Files
    'the different formats below are specificied with "xls", "xlsx" and "xlsm"
    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:="a"
      ActiveWorkbook.Close True
    End If


  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:="a"
        ActiveWorkbook.Close True


      End If


    Next
  Next


  With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
    .EnableEvents = True
    .AskToUpdateLinks = True
  End With


End Sub


However, I now want to build a similar routine that REMOVES the password (i.e. the password is 'a') from all the files.

Any help would be appreciated!

Thanks.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Supply the password when opening, and leave blank when saving (incidentally, there's no need to save when closing given that you just saved it!):

Code:
Set masterWB = Workbooks.Open(wb, password:="a")


      ActiveWorkbook.SaveAs fileName:=Application.ActiveWorkbook.FullName, Password:=""
      ActiveWorkbook.Close
 
Upvote 0
Supply the password when opening, and leave blank when saving (incidentally, there's no need to save when closing given that you just saved it!):

Code:
Set masterWB = Workbooks.Open(wb, password:="a")


      ActiveWorkbook.SaveAs fileName:=Application.ActiveWorkbook.FullName, Password:=""
      ActiveWorkbook.Close


Thanks!

That worked perfectly.
 
Upvote 0

Forum statistics

Threads
1,224,878
Messages
6,181,529
Members
453,053
Latest member
DavidKele

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top