有人知道我将如何在多个子文件夹中打开多个具有不同密码(4个不同密码)的Excel / Word / PFD文件,然后打开文件后,我要保存该文件(相同类型的文件(即Excel / Word / PDF)),使用完全不同的密码?目前,我只能使用一个密码打开一个目录中的文件。
Sub Change password()
Dim MyFolder As String 'Path collected from the folder picker dialog
Dim MyFile As String 'Filename obtained by DIR function
Dim wbk As Workbook 'Used to loop through each workbook
On Error Resume Next
Application.ScreenUpdating = False
'Opens the folder picker dialog to allow user selection
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
MyFile = Dir(MyFolder) 'DIR gets the first file of the folder
'Loop through all files in a folder until DIR cannot find anymore
Do While MyFile <> “”
'Opens the file and assigns to the wbk variable for future use
Set wbk = Workbooks.Open(fileName:=MyFolder & MyFile, Password:="old_password", UpdateLinks:=False)
'Replace the line below with the statements you would want your macro to perform
Application.DisplayAlerts = False
wbk.SaveAs fileName:=MyFolder & MyFile, FileFormat:=xlWorkbookDefault, Password:="new_password", writerespassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
Application.DisplayAlerts = True
wbk.Close
MyFile = Dir 'DIR gets the next file in the folder
Loop
MsgBox "Task Complete!"
Application.ScreenUpdating = True
End Sub