【发布时间】:2018-12-21 13:28:27
【问题描述】:
我在一个文件夹中有大约 400 个 Excel 文件(有些是 .xls ,有些是 .xlsx )。
如何使用 VBA 代码从这些文件中删除密码?
【问题讨论】:
-
这里有一些问答,上面有 vba 代码。
-
密码保护具体是什么?床单是否受到保护? VBA 代码?
我在一个文件夹中有大约 400 个 Excel 文件(有些是 .xls ,有些是 .xlsx )。
如何使用 VBA 代码从这些文件中删除密码?
【问题讨论】:
我猜你知道密码,而且所有文件中的密码都是一样的。
它遍历文件夹中的所有文件,并以cStrExtensions 中指定的扩展名打开每个文件,删除密码,保存并关闭它。
运行将打开文件夹选择器对话框的代码,然后导航到文件所在的文件夹(您看不到它们)并按 OK。
Sub RemovePassword()
' String Lists
Const cStrExtensions As String = "*.xls*"
Const cStrPassword As String = "123"
Dim strFolderPath As String ' Search Folder
Dim strFileName As String ' Current File Name (Workbook)
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
On Error GoTo ProcedureExit
With ThisWorkbook.ActiveSheet
' Choose Search Folder
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = False Then Exit Sub
strFolderPath = .SelectedItems(1) & "\"
End With
' Loop through folder to determine Current File Name (Workbook).
strFileName = Dir(strFolderPath & cStrExtensions)
' Loop through files in folder.
Do While strFileName <> ""
' Open each file in folder
Workbooks.Open strFolderPath & strFileName
With ActiveWorkbook
.Unprotect cStrPassword
.Close True
End With
strFileName = Dir()
' Exclude this workbook.
If .Parent.Name = strFileName Then strFileName = Dir()
Loop
End With
ProcedureExit:
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
【讨论】: