Excel VBA密码保护检查

我有一个项目,我必须在一个文件夹中超过1000多个excel文件,看看哪些是密码保护,哪些不是。 为了节省时间,我写了一个macros来做到这一点,如下:

Sub CheckWbook() Dim Value As String, a As Single, myfolder as string With Application.FileDialog(msoFileDialogFolderPicker) .Show myfolder = .SelectedItems(1) & "\" End With Range("C4") = myfolder Range("B7:C" & Rows.Count) = "" a = 0 Value = Dir(myfolder) Do Until Value = "" If Value = "." Or Value = ".." Then Else If Right(Value, 3) = "xls" Or Right(Value, 4) = "xlsx" Or Right(Value, 4) = "xlsm" Then On Error Resume Next Workbooks.Open Filename:=myfolder & Value, Password:="zzzzzzzzzzzz" If Err.Number > 0 Then Range("C7").Offset(a, 0).Value = "Yes" End If Workbooks(Value).Close False On Error GoTo 0 Range("B7").Offset(a, 0).Value = Value a = a + 1 End If End If Value = Dir Loop End Sub 

我遇到的问题是密码的popup窗口仍然存在:它不填写密码。 任何帮助将不胜感激。 -一个

编辑改变了一下代码,并通过了错误消息,但现在我陷入了密码popup,即停止macros完全正常工作,尽pipeOn Error Resume Nextfunction。

然后,我碰到这个代码,我认为可以帮助:

  Option Explicit Public Sub ProcessBatch() Dim strFileName As String Dim strFilePath As String Dim oDoc As Document ' Set Directory for Batch Process strFilePath = "C:\Test\" ' Get Name of First .doc File from Directory strFileName = Dir$(strFilePath & "*.doc") While Len(strFileName) <> 0 ' Set Error Handler On Error Resume Next ' Attempt to Open the Document Set oDoc = Documents.Open( _ FileName:=strFilePath & strFileName, _ PasswordDocument:="?#nonsense@$") Select Case Err.Number Case 0 ' Document was Successfully Opened Debug.Print strFileName & " was processed." Case 5408 ' Document is Password-protected and was NOT Opened Debug.Print strFileName & " is password-protected " & _ "and was NOT processed." ' Clear Error Object and Disable Error Handler Err.Clear On Error GoTo 0 ' Get Next Document GoTo GetNextDoc Case Else ' Another Error Occurred MsgBox Err.Number & ":" & Err.Description End Select ' Disable Error Handler On Error GoTo 0 '------------------------------------- '------------------------------------- '---Perform Action on Document Here--- '------------------------------------- '------------------------------------- ' Close Document oDoc.Close ' Clear Object Variable Set oDoc = Nothing GetNextDoc: ' Get Next Document from Specified Directory strFileName = Dir$() Wend End Sub 

但是这不能将oDoc识别为文档。 任何想法如何得到它的工作?

打开excel文件? 或工作表

如果是单张应该

ActiveSheet.Unprotect密码:=“yourpassword”

如果这是一个优秀的

ActiveWorkbook.Unprotect( “youtpassword”)

我希望它能为你提供一个拥抱,我在这里学到了很多东西,我希望你也希望能为我提供帮助