从 Excel 个文件中删除密码

Remove password from Excel files

我在一个文件夹中有大约 400 个 Excel 个文件(有些是 .xls,有些是 .xlsx)。

如何使用 VBA 代码从这些文件中删除密码?

从 .xls* 文件中删除工作簿密码

我猜你知道密码,而且它在所有文件中都是相同的。

如何:

它遍历文件夹中的所有文件并打开每个文件,每个文件都带有 cStrExtensions 中指定的扩展名,删除密码,保存并关闭它。

用法:

运行 将打开文件夹选择器对话框的代码,然后导航到文件所在的文件夹(您看不到它们)并按确定。

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