运行时错误438不会触发错误处理程序

时间:2017-08-13 21:56:52

标签: vba

我正在尝试编写一个代码,它将遍历Windows shell,直到找到Internet Explorer,它将包含一个包含该类名的网页。问题是代码如果无法设置adauga_pariu它会给我438运行时错误并且不会触发错误处理程序。

i = 0


Set shellWins = New ShellWindows

If shellWins.Count > 0 Then
  For i = 0 To shellWins.Count

  Set ie = shellWins.Item(i)

  On Error GoTo error

  Set adauga_ron = ie.document.getElementsByClassName("KambiBC-outcome-item")

    If adauga_ron.Length > 0 Then
       GoTo ok    
    End If

error:
    i = i + 1
  Next i

End If

ok:

1 个答案:

答案 0 :(得分:2)

您没有正确处理错误。处理后,您需要Resume,否则您仍处于“错误处理”模式,无法处理任何新错误。

'i = 0  '<-- this isn't needed - the "For i = 0" will initialise i to 0
Set shellWins = New ShellWindows
If shellWins.Count > 0 Then
    For i = 0 To shellWins.Count
        Set ie = shellWins.Item(i)

        On Error GoTo error
        Set adauga_ron = ie.document.getElementsByClassName("KambiBC-outcome-item")

        If adauga_ron.Length > 0 Then
            On Error GoTo 0 ' To avoid having later errors coming back to this code
            GoTo ok    
        End If
        On Error GoTo 0 'Always disable error handling when you don't need it
errorContinue:
        'i = i + 1 '<-- don't do this - you are already using a "For i" loop
    Next i
End If
MsgBox "No match found"
Exit Sub

error:
    Resume errorContinue 'Always "Resume" from your error handler

ok: