XMLHTTP onTimeOut时如何进行VBA回调函数?

时间:2012-07-10 05:15:47

标签: excel vba callback timeout msxml

我正在尝试从网络服务器获取xml数据到excel,然后我写了一个sendRequest函数来调用excel

=sendRequest("http://abb.com/index.php?id=111")

当网络服务器出现问题,无法连接或无法找到时,excel没有响应,这太可怕了!为了避免它,我认为我们应该设置timeOut。这些是我的功能:

Function sendRequest(Url)
    'Call service
    Set XMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")

    'Timeout values are in milli-seconds
    lResolve = 10 * 1000
    lConnect = 10 * 1000
    lSend = 10 * 1000
    lReceive = 15 * 1000 'waiting time to receive data from server
    XMLHTTP.setTimeOuts lResolve, lConnect, lSend, lReceive

    XMLHTTP.OnTimeOut = OnTimeOutMessage 'callback function

    XMLHTTP.Open "GET", Url, False

    On Error Resume Next
    XMLHTTP.Send
    On Error GoTo 0

    sendRequest = (XMLHTTP.responseText)
End Function

Private Function OnTimeOutMessage()
    'Application.Caller.Value = "Server error: request time-out"
    MsgBox ("Server error: request time-out")
End Function

通常,当XMLHTTP超时发生时,将执行事件OnTimeOutMessage(引用#1#2)。但与my test一样,OnTimeOutMessagesendRequest()

的开头执行

Msxml2.ServerXMLHTTP.6.0 请求超时时,如何使用回调函数?

感谢您的帮助!

2 个答案:

答案 0 :(得分:5)

该行;

XMLHTTP.OnTimeOut = OnTimeOutMessage

不是方法分配;而是立即执行OnTimeOutMessage()(并将其无用的返回值分配给OnTimeOut)。

JavaScript 中的等效行根据您的示例链接正确地将Function对象分配给OnTimeOut以便后续调用 - VBA不支持此。

相反,您可以捕获.send之后引发的超时错误或使用早期绑定WithEvents和&内联事件处理程序。

答案 1 :(得分:3)

Tim是正确的,如果你有超时,那么每个请求都会“挂起”Excel / VBA,直到超时持续时间结束,然后再继续。使用async将允许您多次请求而无需长时间请求延迟响应或进一步请求。

status属性表示请求返回的HTTP状态代码。只需将下面的代码放在现有代码中,以便进行较慢的同步检查,或将响应处理移动到异步的事件处理程序。

XMLHTTP.Send

If XMLHTTP.Status = "200" Then
    '200      OK
    htmlString = XMLHTTP.ResponseText
Elseif XMLHTTP.Status = "408" Then
    '408      Request Timeout
    Call OnTimeOutMessage
else
    'All status return values
    'Number      Description
    '100      Continue
    '101      Switching protocols
    '200      OK
    '201      Created
    '202      Accepted
    '203      Non-Authoritative Information
    '204      No Content
    '205      Reset Content
    '206      Partial Content
    '300      Multiple Choices
    '301      Moved Permanently
    '302      Found
    '303      See Other
    '304      Not Modified
    '305      Use Proxy
    '307      Temporary Redirect
    '400      Bad Request
    '401      Unauthorized
    '402      Payment Required
    '403      Forbidden
    '404      Not Found
    '405      Method Not Allowed
    '406      Not Acceptable
    '407      Proxy Authentication Required
    '408      Request Timeout
    '409      Conflict
    '410      Gone
    '411      Length Required
    '412      Precondition Failed
    '413      Request Entity Too Large
    '414      Request-URI Too Long
    '415      Unsupported Media Type
    '416      Requested Range Not Suitable
    '417      Expectation Failed
    '500      Internal Server Error
    '501      Not Implemented
    '502      Bad Gateway
    '503      Service Unavailable
    '504      Gateway Timeout
    '505      HTTP Version Not Supported

End If