Excel VBA密码保护检查

时间:2014-01-09 14:28:37

标签: excel excel-vba password-protection vba

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

    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

我遇到的问题是密码的弹出窗口仍然存在:它没有填写密码。任何帮助将受到高度赞赏。 -A

修改 稍微更改了代码,并且错过了错误消息,但是现在我遇到了密码弹出窗口,这会阻止宏完全工作,尽管On Error Resume Next功能。

然后,我遇到了这个我认为可以提供帮助的代码:

    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识别为文档。关于如何使其发挥作用的任何想法?

1 个答案:

答案 0 :(得分:0)

打开excel文件?或表

如果它是一张纸应该是

ActiveSheet.Unprotect密码:=“yourpassword”

如果是excel

ActiveWorkbook.Unprotect( “youtpassword”)

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