excel - VBA Asynchronous data call with HTTP -
i have made handy vba function returns http status of given url using msxml2.serverxmlhttp object. function executed synchronously , rest of code freezes until request resolved. how turn asynch call?
current working function:
function page_http_status(url) string dim xmlhttp object set xmlhttp = createobject("msxml2.serverxmlhttp") xmlhttp.open "get", url, false xmlhttp.setrequestheader "content-type", "application/x-www-form-urlencoded" xmlhttp.setrequestheader "user-agent", "mozilla/5.0 (windows nt 6.1; rv:25.0) gecko/20100101 firefox/25.0" xmlhttp.send page_http_status = xmlhttp.status end function
onreadystatechange attempt
i know there xmlhttp.onreadystatechange event can trigger named callback function, how can trigger function return value page_http_status()?
xmlhttp.onreadystatechange = functionreadystatechange
and then
function functionreadystatechange () msgbox ("request received! " & xmlhttp.status) end function
after further reading realised vba not support callback functions out of box.
using xmlhttp.onreadystatechange viable solution require work around 2 limitations:
1) xmlhttp have declared publicly in order keep object's scope available "callback" (this fine).
2) original function not return value have save output in procedure (instead of "scalar" function returns output).
Comments
Post a Comment