我希望设置超时值并在此事件发生时使用VBA和Excel捕获此事件。到目前为止,我尝试了XMLHTTP60和WinHttpRequest:
Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
With XMLHTTP
.setTimeouts 11, 11, 11, 11 'Values for testing to get a timeout
'.setTimeouts TIMEOUT_LRESOLVE, TIMEOUT_LCONNECT, TIMEOUT_LSEND, TIMEOUT_LRECEIVE
.Open "GET", currenturl, False
.send
'.waitForResponse 1 'testing
If .responseText Like "*PATH_NOT_FOUND*" Or .responseText Like "*OUTSIDE_BOUNDS*" Then
noroutefound = True
GoTo skiploop
End If
str = Split(.responseText, "{""startTime"":")
complstr = .responseText
End With但是,我无法捕获由超时或超时本身引起的错误。
我怎样才能赶上超时?
发布于 2019-04-30 20:57:35
在预期错误之前和errHand中添加On Error GoTo errHand的错误处理:
'Code
On Error GoTo errHand:
'Code producing error
Exit Sub
errHand
If err.Number <> 0 Then
Debug.Print err.Message
End If
Exit Sub如果您有感兴趣的错误号,则可以使用select case err.Number以定制的方式处理特定的错误,例如:
errHand:
Select Case Err.Number
Case -2147012894 'Code for Timeout
Msgbox "Timeout"
Case -2147012891 'Code for Invalid URL
Msgbox "Invalid URL"
Case Else 'Add more Errorcodes here if wanted
MsgBox "Errornumber: " & Err.Number & vbnewline & "Errordescription: " & Error(Err.Number)
End selecthttps://stackoverflow.com/questions/55920463
复制相似问题