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

问题描述:

我正在尝试编写一个代码,这个代码将遍历windows shell直到找到Internet Explorer,它将包含一个包含该类名的网页。问题是,代码,如果它不能设置adauga_pariu它会给我438运行时错误,并不会触发错误处理程序。运行时错误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: 

您没有正确处理您的错误。一旦你处理了它们,你需要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: 
+0

谢谢,这是做的工作,唯一的就是即使迭代遍历所有我并且没有设置adauga_pariu,它会继续。 – Rius2

+0

您的代码在'ok:'标签后自动放入代码中。如果你不想这样做,只要改变'如果shellWins.Count> 0 Then'End If'后处理的'GoTo ok'语句与任何你想做的事情,如果没有匹配。 (也许是'Exit Sub'?) – YowE3K