How to VBA sends an Async XMLHTTP request?

后端 未结 2 1194
小蘑菇
小蘑菇 2020-12-18 12:32

To get data from a webservice in Excel, I have created an excel addin provide function to get it; user just need type in cell: =sendRequest(\"http://webservice.com\")<

相关标签:
2条回答
  • 2020-12-18 13:03

    As the downside of the solution stated here, the proper way to get the async return value for the function is to 1) cache your request's url => returned value to a collection/dictionary, and then 2) refresh your formula

    0 讨论(0)
  • 2020-12-18 13:12

    This is my solution file for your question; indeed it is updated from your async test file.

    Based on this discusion, you can change the display text of cell without changing its formula by using Range(yourCellAddress).NumberFormat = "0;0;0;""Value to display"""

    So to your question, the solution is

    1. In your sendAsyncRequest function replace the return line as

      sendAsyncRequest = "anything other than numbers"

    2. In your ReadyStateChangeHandler sub, replace

      Application.Range(cellAddress).Value = XMLHttpReq.responseText 'return responseText to cell

    by

    cellDisplayText = XMLHttpReq.responseText
    Range(cellAddress).NumberFormat = "0;0;0;""" & cellDisplayText & """"
    
    0 讨论(0)
提交回复
热议问题