从VBA代码进行异步Rest调用的最佳方法是什么

时间:2019-08-08 08:57:00

标签: excel vba

我想从background的excel宏进行[post / get]调用。宏应该每5秒静默执行一次。如果剩余呼叫得到有效响应,请向用户显示一些消息。
注意:当宏在后台Async中运行时,它不应中断excel上的用户体验。
我使用了回调方法,但问题是excel速度很慢,因为使rest调用的宏在后台连续运行。下面是代码FYR。

'MyReadyStateHandler class module
Sub OnReadyStateChange()    
DoEvents    
If Actions.docx.readyState = 4 Then    
If Actions.docx.Status = 200 Then    
IsReadyState = True    
outPutText = Actions.docx.responseText    
Exit Sub     
Else     
IsReadyState = False    
Exit Sub    
End If    
End If    
End Sub    
_________________________
'Sub using MyReadyStateHandler
Public sub TestRestServiceStatus()   
Set MyOnReadyStateWrapper = New MyReadyStateHandler    
Set docx = New MSXML2.XMLHTTP60    
docx.OnReadyStateChange = MyOnReadyStateWrapper    
docx.Open "POST", urlString, False    
docx.send jsonBody    
responceStatus = MyOnReadyStateWrapper.IsReadyState     
If (responceStatus = True) Then    
Dim notificationOutPut As String 
notificationOutPut=MyOnReadyStateWrapper.outPutText    
If InStr(notificationOutPut , "PROCESSING") > 0 Then    
Call TestRestServiceStatus    
ElseIf  InStr(notificationOutPut , "COMPLETE") > 0 Then    
'Notify User on Excel    
EndIF    
EndIf    
End sub  

0 个答案:

没有答案