从Excel文件中删除密码

问题描述 投票:1回答:1

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

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

excel vba passwords
1个回答
1
投票

Remove Workbook Password From .xls* Files

我想你知道密码,所有文件都是一样的。

怎么样:

它遍历文件夹中的所有文件,并使用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
© www.soinside.com 2019 - 2024. All rights reserved.