0

I am new to class programming in vba.

Here is my first attempt using teaching of :Asynchronous HTTP POST Request in MS Access

I am using word vba.

Here is my CXMLHTTPHandler.cls

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "CXMLHTTPHandler"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Dim m_xmlHttp As MSXML2.XMLHTTP

Public Sub Initialize(ByRef xmlHttpRequest As MSXML2.XMLHTTP)
   Set m_xmlHttp = xmlHttpRequest
End Sub

Sub OnReadyStateChange()
Attribute OnReadyStateChange.VB_UserMemId = 0
   Debug.Print m_xmlHttp.readyState
   If m_xmlHttp.readyState = 4 Then
      If m_xmlHttp.Status = 200 Then
         msgbox m_xmlHttp.responseText
      Else
         msgbox "Something Went Wrong"
     End If
   End If
End Sub

Here is my standard module

Option Explicit
Public xmlHttpRequest As MSXML2.XMLHTTP
Function sasynchreq(url As String)
On Error GoTo FailedState
If Not xmlHttpRequest Is Nothing Then Set xmlHttpRequest = Nothing
Dim MyXmlHttpHandler As CXMLHTTPHandler
Set xmlHttpRequest = New MSXML2.XMLHTTP
'Create an instance of the wrapper class.
Set MyXmlHttpHandler = New CXMLHTTPHandler
MyXmlHttpHandler.Initialize xmlHttpRequest
'Assign the wrapper class object to onreadystatechange.
xmlHttpRequest.OnReadyStateChange = MyXmlHttpHandler
'Get the page stuff asynchronously.
xmlHttpRequest.Open "GET", url, True
xmlHttpRequest.send ""
Exit Function
FailedState:
    msgbox Err.Number & ": " & Err.Description
End Function

Sub test()
    Dim url As String, do_something As String
    url = "http://httpbin.org/html"
    do_something = sasynchreq(url)
    'do somethign with do_something
End Sub

Everything works fine. What if I want to assign httprequest to some variable for example in test sub.?

Community
  • 1
  • 1
Rahul
  • 10,830
  • 4
  • 53
  • 88

2 Answers2

1

Sounds like you're after the Property statements. In your case, it would be Property Get (https://msdn.microsoft.com/en-us/library/office/gg264197.aspx).

In your class module, put this:

Public Property Get HttpRequest() As MSXML2.XMLHTTP
    Set HttpRequest = m_xmlHttp
End Property

This now gives your Module access to the class variable, like so:

Dim rq as MSXML2.XMLHTTP
set rq = MyXmlHttpHandler.HttpRequest
Ambie
  • 4,872
  • 2
  • 12
  • 26
  • Thanks for help. I think one missing part is await callback. – Rahul Jan 30 '17 at 10:19
  • Sorry, I misunderstood your question. Until I read your link I thought that asynchronous tasks weren't possible in VBA - and I've never developed with it. I'm afraid I don't know how to get the equivalent of an `await` result. Perhaps edit your question to show how and when you're trying to get the object - it might even be that you put that part of your code within the `OnReadyStateChange` routine. – Ambie Jan 30 '17 at 11:32
0

I think you can expect the ultimate payload predicated on the Send method be present by the time you exit your function. If so, your function signature should look something like:

Function sasynchreq(url As String) As String

and in the body of the function, there should be a:

xmlHttpRequest.send ""
sasynchreq = xmlHttpRequest.responseXML.xml

or something like that. Then do_something would be available right away for use.

Long story short, if there are no synchronicity issues, the way it's currently written, your function is not returning anything back to do_something. Whatever it is you're returning, the function signature and local variable types inside sub test should match. If you are returning an object, you will need to use the Set keyword in both instances.

VBlades
  • 2,241
  • 1
  • 12
  • 8