sending http request via VBA

sending http request via VBA

mucip
Collaborator Collaborator
1,804 Views
4 Replies
Message 1 of 5

sending http request via VBA

mucip
Collaborator
Collaborator

Hi,

Is there any way to send web request and get answer via VBA?

 

Regards,

Mucip:)

0 Likes
Accepted solutions (2)
1,805 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor
Accepted solution

There is no direct support inside VBA to send/receive http request/response. You would have to use MSXML (6.0 is the latest/last version?). It is an old, obsolete technology by now.

 

You might be able to find third party COM-based http(s) components, such as these:

 

https://www.chilkatsoft.com/HttpActiveX.asp 

https://www.weonlydo.com/HttpDLX/http-web-component.asp 

 

However, if the business solution involves http(s) access, I would not waste time (and money on third party software) on VBA, if I were you. It can be easily done with MS .NET, now that AutoCAD .NET API is the main stream of AutoCAD programming.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

mucip
Collaborator
Collaborator

Dear @norman.yuan ,

Thanks a lot.

Yes, you always suggesting me to jump in to C#  🙂

But I really prefer C++ and don't like C#. By the way my goal is almost finished. I don't want to waste time again my VBA project to C# conversion.

In any way. Thanks again.

And If I need to write something in AutoCAD next time than I will start with C# I promiss 🙂

 

Regards,

Mucip:)

0 Likes
Message 4 of 5

mucip
Collaborator
Collaborator
Accepted solution

Dear @norman.yuan ,

I solved below code with your guidence.

 

Public Function GetServerOK() As String 
    Dim HttpRequest As Object
    Dim data
 
    On Error Resume Next
    'Create the XMLHttpRequest object.
    Set HttpRequest = CreateObject("MSXML2.XMLHTTP")
 
    'Check if the object was created.
    If Err.Number <> 0 Then
        'Return error message.
        GetServerOK= "Could not create the XMLHttpRequest object!"
        'Release the object and exit.
        Set HttpRequest = Nothing
        Exit Function
    End If
    On Error GoTo 0
 
    'Create the request - no special parameters required.
    HttpRequest.Open "POST", "http://XX.YY/met/server.php", False
    
    HttpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    
    data = "info=" & GetMyLine() 
    
    HttpRequest.send data
 
    'Return the result of the request (the IP string).
    GetServerOK= HttpRequest.responseText
 
End Function

 

 

I created simple php file on Debian/Apache and send back echo according to POST value.

 

Source: https://www.codegrepper.com/code-examples/vb/vba+send+post+request+MSXML2.XMLHTTP

 

 

Regards,

Mucip:)

0 Likes
Message 5 of 5

mucip
Collaborator
Collaborator

Hi again,

For Win7, you need to use below line:

 

'For Win10
Set HttpRequest = CreateObject("MSXML2.XMLHTTP")
'For Win7 & working Win10 also
Set HttpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")

 

Regards,

Mucip:)

0 Likes