I need to give access to all my Excel 2003 workbooks (100's of files and my job is surplus to requirements). I always used one of two passwords on all workbooks for both file Open and WriteRes. However I don't want to give my passwords (which would be the simplist solution).
What I would like to do is use VBA to open each workbook using my password(s), then change both Open and WriteRes password(s), then save as same filename overwriting the original file.
My biggest problem is I'm a beginner at VBA and while I 'get' somethings to work I know I don't always understand how or why it works!
I have searched and found a macro the extracts a list of filenames as text and puts them in column A of Sheet named "Files"
By running the code below I can open a hard coded named file using most used password first, if it fails then second password, then resaving the file with a new password overwriting the original file.
But I don't know and can't find how to use the original list of file names starting at row 1 and incrementing to row 2 etc in place of the hard coded filename.
I suspect that I'm going the long way round making the list of filenames first and then using it to open the file (I mean if I'm capturing the filename as text in the first piece of code, I should be able to open the file at that point, change the password, and resave it overwriting the file). I also suspect I'm a little short of 'error trapping' in case I come across file(s) that have neither password 'abc' or 'def'... <?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
Any and all help welcome and appreciated!<o></o>
p.s. 'abc', 'def' and 'xyz' are not my real passwords!
What I would like to do is use VBA to open each workbook using my password(s), then change both Open and WriteRes password(s), then save as same filename overwriting the original file.
My biggest problem is I'm a beginner at VBA and while I 'get' somethings to work I know I don't always understand how or why it works!
I have searched and found a macro the extracts a list of filenames as text and puts them in column A of Sheet named "Files"
Rich (BB code):
Public Sub Auto_Open()
Dim j As Integer
Dim i As Long
Sheets("Files").Columns("A:A").ClearContents
With Application.FileSearch
.LookIn = "C:\TestBed\TestExtractFileNames"
.FileType = msoFileTypeExcelWorkbooks
.SearchSubFolders = True
.Execute
j = 1
For i = 1 To .FoundFiles.Count
ActiveWorkbook.Worksheets("Files").Cells(j, 1) = .FoundFiles(i)
j = j + 1
Next i
End With
End Sub
By running the code below I can open a hard coded named file using most used password first, if it fails then second password, then resaving the file with a new password overwriting the original file.
But I don't know and can't find how to use the original list of file names starting at row 1 and incrementing to row 2 etc in place of the hard coded filename.
Rich (BB code):
Sub OpenPasswordProtectedFile()
Screenupdate = False 'Stops screen updating & speed up macro
On Error Resume Next 'If file password #1 is incorrect, this moves macro onto next line to open with password #2 without flagging error and requesting input.
Workbooks.Open Filename:="C:\TestBed\TestExtractFileNames\XXX.xls", UpdateLinks:=2, Password:="abc", WriteResPassword:="abc" 'Opens listed Excel workbook without updating links with password "abc" to both open and write.
Workbooks.Open Filename:="C:\TestBed\TestExtractFileNames\XXX.xls", UpdateLinks:=2, Password:="def", WriteResPassword:="def" 'Opens listed Excel workbook without updating links with password "def" to both open and write.
Application.DisplayAlerts = False 'Stops Excel requesting confirmation to replace existing file of same name.
ActiveWorkbook.SaveAs Filename:="C:\TestBed\TestExtractFileNames\XXX.xls", Password:="xyz", WriteResPassword:="xyz" 'Re-saves listed Excel workbook with password "xyz" to both open and write.
ActiveWorkbook.Close 'Closes file
End Sub
I suspect that I'm going the long way round making the list of filenames first and then using it to open the file (I mean if I'm capturing the filename as text in the first piece of code, I should be able to open the file at that point, change the password, and resave it overwriting the file). I also suspect I'm a little short of 'error trapping' in case I come across file(s) that have neither password 'abc' or 'def'... <?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
Any and all help welcome and appreciated!<o></o>
p.s. 'abc', 'def' and 'xyz' are not my real passwords!