Code Stopping While Looping through files on workbook.close

谁都会走 提交于 2021-02-09 07:04:34

问题


I am trying to loop through Excel files, open them, run some code that breaks passwords then closes the workbook and moves to the next.

My code works on most of my files. I am having trouble with files that have macros in them. (That is the only thing I can see that differentiates these files from the others.)

I have noticed that with the problem files when I open them my wb variable is set to nothing. It still opens the file and my code continues to run but when I execute the line wb.close my code just stops. No error message but it doesn't finish the loop it is in.

Not sure if there is a way to attach a file that works and one that doesn't but I can if someone can explain how to do this.

When I open a file that doesn't cause this problem, in the locals window when I expand the variable wb, it has other attributes. On the problem files when I expand the wb variable it just says: no variables

When I open one of these files without using VBA I get a warning that it contains a possible security concern and that macros have been disabled. I think that is where my problem is coming from however I thought I as handling this with Application.AutomationSecurity = msoAutomationSecurityForceDisable.

I have updated my code to the following but it has not solved the problem of stopping the code on wb.close

Do While fileName <> vbNullString

    Set wb = Workbooks.Open(fileName:=directory & fileName, _
                            UpdateLinks:=0, _
                            IgnoreReadOnlyRecommended:=True, _
                            Notify:=False, _
                            CorruptLoad:=xlNormalLoad)
    If Err.Number = 0 And Not wb Is Nothing Then
        On Error GoTo 0
        Call AllInternalPasswords
        wb.Close True
        fileName = Dir()
    Else
        Err.Clear
        On Error GoTo 0
    End If
Loop

Sub TestPasswordLoop()

Dim directory As String, fileName As String, i As Variant, wb As Workbook
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Dim security As MsoAutomationSecurity
security = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable

directory = "C:\Users\seth\Desktop\Files for Testing\"
fileName = Dir(directory & "*.xl??")

i = 0
Do While fileName <> vbNullString
    On Error Resume Next
    'Set wb = Workbooks.Open(fileName:=directory & fileName)
    Set wb = Workbooks.Open(fileName:=directory & fileName, _
                            UpdateLinks:=0, _
                            IgnoreReadOnlyRecommended:=True, _
                            Notify:=False, _
                            CorruptLoad:=xlNormalLoad)

    Call AllInternalPasswords 'this code is below
    wb.Close True
    i = i + 1
    Application.StatusBar = "Files Completed:  " & i
    fileName = Dir()
Loop

Application.AutomationSecurity = security
Application.StatusBar = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "Complete"

End Sub

Public Sub AllInternalPasswords()
    ' Breaks worksheet and workbook structure passwords. Bob McCormick
    '  probably originator of base code algorithm modified for coverage
    '  of workbook structure / windows passwords and for multiple passwords
    '
    ' Norman Harker and JE McGimpsey 27-Dec-2002 (Version 1.1)
    ' Modified 2003-Apr-04 by JEM: All msgs to constants, and
    '   eliminate one Exit Sub (Version 1.1.1)
    ' Reveals hashed passwords NOT original passwords

    Application.DisplayAlerts = False
    'Application.ScreenUpdating = False

    Const DBLSPACE As String = vbNewLine & vbNewLine
    Const AUTHORS As String = DBLSPACE & vbNewLine & _
            "Adapted from Bob McCormick base code by" & _
            "Norman Harker and JE McGimpsey"
    Const HEADER As String = "AllInternalPasswords User Message"
    Const VERSION As String = DBLSPACE & "Version 1.1.1 2003-Apr-04"
    Const REPBACK As String = DBLSPACE & "Please report failure " & _
            "to the microsoft.public.excel.programming newsgroup."
    Const ALLCLEAR As String = DBLSPACE & "The workbook should " & _
            "now be free of all password protection, so make sure you:" & _
            DBLSPACE & "SAVE IT NOW!" & DBLSPACE & "and also" & _
            DBLSPACE & "BACKUP!, BACKUP!!, BACKUP!!!" & _
            DBLSPACE & "Also, remember that the password was " & _
            "put there for a reason. Don't stuff up crucial formulas " & _
            "or data." & DBLSPACE & "Access and use of some data " & _
            "may be an offense. If in doubt, don't."
    Const MSGNOPWORDS1 As String = "There were no passwords on " & _
            "sheets, or workbook structure or windows." & AUTHORS & VERSION
    Const MSGNOPWORDS2 As String = "There was no protection to " & _
            "workbook structure or windows." & DBLSPACE & _
            "Proceeding to unprotect sheets." & AUTHORS & VERSION
    Const MSGTAKETIME As String = "After pressing OK button this " & _
            "will take some time." & DBLSPACE & "Amount of time " & _
            "depends on how many different passwords, the " & _
            "passwords, and your computer's specification." & DBLSPACE & _
            "Just be patient! Make me a coffee!" & AUTHORS & VERSION
    Const MSGPWORDFOUND1 As String = "You had a Worksheet " & _
            "Structure or Windows Password set." & DBLSPACE & _
            "The password found was: " & DBLSPACE & "$$" & DBLSPACE & _
            "Note it down for potential future use in other workbooks by " & _
            "the same person who set this password." & DBLSPACE & _
            "Now to check and clear other passwords." & AUTHORS & VERSION
    Const MSGPWORDFOUND2 As String = "You had a Worksheet " & _
            "password set." & DBLSPACE & "The password found was: " & _
            DBLSPACE & "$$" & DBLSPACE & "Note it down for potential " & _
            "future use in other workbooks by same person who " & _
            "set this password." & DBLSPACE & "Now to check and clear " & _
            "other passwords." & AUTHORS & VERSION
    Const MSGONLYONE As String = "Only structure / windows " & _
             "protected with the password that was just found." & _
             ALLCLEAR & AUTHORS & VERSION & REPBACK
    Dim w1 As Worksheet, w2 As Worksheet
    Dim i As Integer, j As Integer, k As Integer, l As Integer
    Dim m As Integer, n As Integer, i1 As Integer, i2 As Integer
    Dim i3 As Integer, i4 As Integer, i5 As Integer, i6 As Integer
    Dim PWord1 As String
    Dim ShTag As Boolean, WinTag As Boolean

    Application.ScreenUpdating = False
    With ActiveWorkbook
        WinTag = .ProtectStructure Or .ProtectWindows
    End With
    ShTag = False
    For Each w1 In Worksheets
            ShTag = ShTag Or w1.ProtectContents
    Next w1
    If Not ShTag And Not WinTag Then
        'MsgBox MSGNOPWORDS1, vbInformation, HEADER
        Exit Sub
    End If
    'MsgBox MSGTAKETIME, vbInformation, HEADER
    If Not WinTag Then
        'MsgBox MSGNOPWORDS2, vbInformation, HEADER
    Else
      On Error Resume Next
      Do      'dummy do loop
        For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
        For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
        For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
        For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
        With ActiveWorkbook
          .Unprotect Chr(i) & Chr(j) & Chr(k) & _
             Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
             Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
          If .ProtectStructure = False And _
          .ProtectWindows = False Then
              PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
                Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
                Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
              'MsgBox Application.Substitute(MSGPWORDFOUND1, _
                    "$$", PWord1), vbInformation, HEADER
              Exit Do  'Bypass all for...nexts
          End If
        End With
        Next: Next: Next: Next: Next: Next
        Next: Next: Next: Next: Next: Next
      Loop Until True
      On Error GoTo 0
    End If
    If WinTag And Not ShTag Then
      'MsgBox MSGONLYONE, vbInformation, HEADER
      Exit Sub
    End If
    On Error Resume Next
    For Each w1 In Worksheets
      'Attempt clearance with PWord1
      w1.Unprotect PWord1
    Next w1
    On Error GoTo 0
    ShTag = False
    For Each w1 In Worksheets
      'Checks for all clear ShTag triggered to 1 if not.
      ShTag = ShTag Or w1.ProtectContents
    Next w1
    If ShTag Then
        For Each w1 In Worksheets
          With w1
            If .ProtectContents Then
              On Error Resume Next
              Do      'Dummy do loop
                For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
                For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
                For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
                For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
                .Unprotect Chr(i) & Chr(j) & Chr(k) & _
                  Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
                  Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
                If Not .ProtectContents Then
                  PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
                    Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
                    Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
                  'MsgBox Application.Substitute(MSGPWORDFOUND2, _
                        "$$", PWord1), vbInformation, HEADER
                  'leverage finding Pword by trying on other sheets
                  For Each w2 In Worksheets
                    w2.Unprotect PWord1
                  Next w2
                  Exit Do  'Bypass all for...nexts
                End If
                Next: Next: Next: Next: Next: Next
                Next: Next: Next: Next: Next: Next
              Loop Until True
              On Error GoTo 0
            End If
          End With
        Next w1
    End If
    'MsgBox ALLCLEAR & AUTHORS & VERSION & REPBACK, vbInformation, HEADER

    'Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

回答1:


For Error Checking:

Try Saving the Workbook before Saving

Application.DisplayAlerts  = False
     wb.Save
     wb.Close True
Application.DisplayAlerts  = True

For Error Checking:

Try setting your Error Trapping to "Break on All Errors". (In the VBA Editor: Tools > Options> General > Break on All Errors)

Your "On Error Resume Next" is hiding the error



来源:https://stackoverflow.com/questions/53840345/code-stopping-while-looping-through-files-on-workbook-close

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!