Nick and Zone,
Thanks to your help, I was able to get this to work. The solution uses a
password-protected workbook that starts from the Office Manager's XLStart
folder. The top-level sheet, "Macros", displays a list of two macros,
ResetPasswords, and LoadFile.
The first uses a Userform to get the old and new passwords (confirming
each), and the folder location, which has a default value but can be changed
with a command button. The user can also choose whether to change blank
passwords to the new value or leave it blank.
The code attached to the OK button writes the new password to the
password-protected sheet so that its value is available to the LoadFile
macro, which supplies the password to any file she wishes to open, then loops
through all the files in the chosen folder and subfolder, changing the
password as appropriate, and writing all files to a section of the worksheet,
with the action taken for each.
Thanks again for your help.
Sprinks
Private Sub cmdOK_Click()
' Requires:
' - Function GetFolderName
On Error GoTo ErrHandler
' Declare Constants
' This sub writes filenames and results to a cell block.
' Currently it will start at F6:G6.
Const cintRow As Integer = 6 ' Row 6
Const cintCol As Integer = 6 ' Column F
' Declare variables.
Dim fs As FileSearch
Dim wb As Workbook
Dim astrParsedName() As String
Dim i As Integer
Dim strMsg As String
Dim strOld, strNew As String
Dim strFileName As String
Dim strResponse As String
Dim blnChangeBlank As Boolean
Dim astrResult(1 To 3) As String
'Validate form data
If Me![txtOPW] = "" Then
MsgBox "No value entered for the old password.", , "Invalid Data"
Me![txtOPWV] = ""
Me![txtOPW].SetFocus
GoTo ErrExit
End If
If Me![txtOPW] <> Me![txtOPWV] Then
MsgBox "Old passwords do not match.", , "Invalid Data"
Me![txtOPWV] = ""
With Me!txtOPW
.Value = ""
.SetFocus
End With
GoTo ErrExit
End If
If (IsNull(Me![txtNPW]) Or Me![txtNPW] = "") Then
strNew = ""
strResponse = MsgBox("No value entered for the new password. Press
" & _
"OK to remove all passwords or Cancel to exit.", _
vbOKCancel + vbDefaultButton2 + vbCritical,
"Remove All Passwords?")
If strResponse = vbCancel Then
Me![txtNPWV] = ""
Me![txtNPW].SetFocus
GoTo ErrExit
End If
Else: strNew = Me![txtNPW]
End If
If Me![txtNPW] <> Me![txtNPWV] Then
MsgBox "New passwords do not match.", , "Invalid Data"
Me![txtNPWV] = ""
With Me![txtNPW]
.Value = ""
.SetFocus
End With
GoTo ErrExit
End If
' Form data is OK, initialize variables
astrResult(1) = "PW Changed"
astrResult(2) = "PW Blank"
astrResult(3) = "Couldn't Open"
strOld = Me![txtOPW]
' strNew Initialized above
blnChangeBlank = Me![chkBlank]
Application.EnableEvents = False
' Initialize filesearch object.
Set fs = Application.FileSearch
' Set folder to search, subfolders, and filter
With fs
.LookIn = Me![txtFolderName]
.Filename = "*.xls"
.SearchSubFolders = True
End With
If fs.LookIn = "" Then
GoTo ErrExit
End If
' Execute the file search, and check to see if the file(s) are present.
If fs.Execute() > 0 Then
' Write new password to sheet, Cell B50
Me.Hide
ActiveWorkbook.Worksheets("Splash").Activate
With ActiveWorkbook.Worksheets("Macros")
.Unprotect Password:=strOld
.Cells(50, 2).Value = strNew
End With
' Set new password for AdminMacros workbook
With ActiveWorkbook
.Password = strNew
.Worksheets("Macros").Protect Password:=strNew
.Worksheets("Splash").Visible = True
End With
' Turn off screen updating while opening files
Application.ScreenUpdating = False
' Write headings to worksheet
Workbooks("AdminMacros.xls").Sheets("Macros").Cells(cintRow - 1, _
cintCol).Value = "Target Files"
Workbooks("AdminMacros.xls").Sheets("Macros").Cells(cintRow - 1, _
cintCol + 1).Value = "Result"
' Write all filenames to worksheet
For i = 1 To fs.FoundFiles.Count
' Get filename without path; write to worksheet
astrParsedName = Split(fs.FoundFiles.Item(i), "\")
strFileName = astrParsedName(UBound(astrParsedName))
Workbooks("AdminMacros.xls").Sheets("Macros").Cells(cintRow + i
- 1, _
cintCol).Value = strFileName
Next i
' Attempt to open workbook with the supplied password.
' If successful, the password could either match or be blank.
' If the workbook has a password or if user selected blank files,
' reset the password to the new value.
For i = 1 To fs.FoundFiles.Count
On Error Resume Next
Set wb = Nothing
Set wb = Workbooks.Open _
(Filename:=fs.FoundFiles.Item(i), Password:=strOld)
On Error GoTo ErrHandler
If wb Is Nothing Then
' File has a different password, write status to worksheet
Workbooks("AdminMacros.xls").Sheets("Macros").Cells(cintRow
+ i - 1, _
cintCol + 1).Value = astrResult(3)
Else
With ActiveWorkbook
If (.HasPassword Or blnChangeBlank = True) Then
Workbooks("AdminMacros.xls").Sheets("Macros").Cells(cintRow + i - 1, _
cintCol + 1).Value = astrResult(1)
.Password = strNew
.Save
Else
' Password was blank and user doesn't want to change
it
Workbooks("AdminMacros.xls").Sheets("Macros").Cells(cintRow + i - 1, _
cintCol + 1).Value = astrResult(2)
End If
.Close
End With
End If
Next i
Unload Me
With ActiveWorkbook
.Worksheets(1).Activate
.Worksheets("Splash").Visible = False
.Save
End With
Else
' Display message if no files were found.
MsgBox "No files were found in: " & vbCrLf & vbCrLf _
& fs.LookIn, vbOKOnly, "No Files Found!"
End If
ErrExit:
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set fs = Nothing
Set wb = Nothing
Exit Sub
SubExit:
Set fs = Nothing
Application.EnableEvents = True
ErrHandler:
MsgBox "There has been the following error. Please contact the macro "
& _
"administrator." & _
vbCrLf & vbCrLf & Err.Number & vbCrLf & " " & Err.Description
Resume ErrExit
End Sub