7

WebサーバーからExcelにxmlデータを取得しようとしています。次に、sendRequestExcelで呼び出す関数を作成しました。

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

Webサーバーに問題がある、接続できない、または見つからない、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)。しかし、私のテストのようにOnTimeOutMessage、の最初に実行されますsendRequest()

Msxml2.ServerXMLHTTP.6.0リクエストがタイムアウトしたときにコールバック関数を使用するにはどうすればよいですか?

お手伝いありがとうございます!

4

2 に答える 2

5

この線;

XMLHTTP.OnTimeOut = OnTimeOutMessage

メソッドの割り当てではありません。むしろそれはすぐに実行されOnTimeOutMessage()ます(そしてその役に立たない戻り値をに割り当てますOnTimeOut)。

リンクの例にあるJavaScriptの同等の行は、後続の呼び出しのためにFunctionオブジェクトを正しく割り当てますOnTimeOut。これはVBAではサポートされていません。

.send代わりに、アーリーバインディングWithEvents、、、およびインラインイベントハンドラーの後に発生するタイムアウトエラーをトラップするか、使用することができます。

于 2012-07-10T09:47:55.147 に答える
3

タイムアウトがある場合、続行する前にタイムアウト期間が経過するまで、各リクエストがExcel/VBAを「ハング」させるという点でTimは正しいです。非同期を使用すると、長いリクエストで応答や追加のリクエストを遅らせることなく、複数のリクエストを実行できます。

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
于 2012-07-10T18:34:23.033 に答える