如何在XMLHTTP onTimeOut时VBA回调函数? [英] How to VBA callback function when XMLHTTP onTimeOut?
问题描述
我尝试从webserver到excel获取xml数据,然后我写了一个 sendRequest
函数调用excel
= sendRequest(http://abb.com/index.php?id=111)
当web服务器有麻烦,不能连接或找不到,excel没有反应,这是可怕的!为了避免它,我想我们应该设置timeOut。这些是我的函数:
函数sendRequest(Url)
调用服务
设置XMLHTTP = CreateObject Msxml2.ServerXMLHTTP.6.0)
'超时值以毫秒为单位
lResolve = 10 * 1000
lConnect = 10 * 1000
lSend = 10 * 1000
lReceive = 15 * 1000'从服务器接收数据的等待时间
XMLHTTP.setTimeOuts lResolve,lConnect,lSend,lReceive
XMLHTTP.OnTimeOut = OnTimeOutMessage'回调函数
XMLHTTP.OpenGET,Url,False
错误恢复下一个
XMLHTTP.Send
错误时转到0
sendRequest =(XMLHTTP.responseText)
结束函数
私有函数OnTimeOutMessage()
'Application.Caller.Value =服务器错误:请求超时
MsgBox(服务器错误:请求超时)
结束函数
当发生 XMLHTTP
的超时时,将执行事件 OnTimeOutMessage
(引用#1 ,#2 )。但是,在我的测试中, OnTimeOutMessage
在 sendRequest()开始执行
行;
XMLHTTP.OnTimeOut = OnTimeOutMessage
不是方法分配;而是立即执行 OnTimeOutMessage()
(并将其无用的返回值分配给 OnTimeOut
)。
根据您的示例链接, JavaScript 中的等价行正确地将 但是,你可以捕获 I'm trying get xml data from webserver to excel, then I wrote a When web-server having trouble, cannot connect or cannot find, excel is not responding, it was horrible! To avoid it, i think we should set timeOut. These are my function: Normally, when How to use callback function when Msxml2.ServerXMLHTTP.6.0 request is time-out? Thank for your help! The line; Is not a method assignment; rather it immediately executes The equivalent line in JavaScript as per your example link correctly assigns a Instead, you could trap the timeout error raised after 这篇关于如何在XMLHTTP onTimeOut时VBA回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Function
对象分配给
.send
或使用早期绑定, WithEvents
,&内联事件处理程序。sendRequest
function to call in excel=sendRequest("http://abb.com/index.php?id=111")
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
's timeout occurs, event OnTimeOutMessage
will be executed (reference #1, #2). But as in my test, OnTimeOutMessage
is executed right at the beginning of sendRequest()
XMLHTTP.OnTimeOut = OnTimeOutMessage
OnTimeOutMessage()
(and assigns its useless return value to OnTimeOut
).Function
object to OnTimeOut
for subsequent invokation - this is not supported by VBA..send
or use early binding, WithEvents
, & inline event handlers.