19

I tried to follow this example: http://libkod.info/officexml-CHP-9-SECT-5.shtml - Archive.org - Donate

but it gave this error

Imgur

on this line:

Dim objHTTP As New MSXML2.XMLHTTP

I tried to use this example: How can I send an HTTP POST request to a server from Excel using VBA?

but it gave this error:

Imgur

on this line:

Print objHTTP.Status

So how do I make a POST REST call in VBA? How do I make a PUT multi-part/form-data file upload REST call in VBA?

Tools > References

Imgur

Code

Sub SendEmail()
    'Dim objHTTP As New MSXML2.XMLHTTP
    'Set objhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URL = "http://localhost:8888/rest/mail/send"
    objHTTP.Open "POST", URL, False
    objHTTP.send ("{""key"":null,""from"":""me@me.com"",""to"":null,""cc"":null,""bcc"":null,""date"":null,""subject"":""My Subject"",""body"":null,""attachments"":null}")
    Print objHTTP.Status
    Print objHTTP.ResponseText
    
End Sub

Reference

WinHttpRequest object: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384106(v=vs.85).aspx

Community
  • 1
  • 1
Chloe
  • 25,162
  • 40
  • 190
  • 357

4 Answers4

15

You probably haven't added a reference to Microsoft XML (any version) for Dim objHTTP As New MSXML2.XMLHTTP in the VBA window's Tools/References... dialog.

Also, it's a good idea to avoid using late binding (CreateObject...); better to use early binding (Dim objHTTP As New MSXML2.XMLHTTP), as early binding allows you to use Intellisense to list the members and do all sorts of design-time validation.

Monty Wild
  • 3,981
  • 1
  • 21
  • 36
  • Ok I added those references and added a screenshot, but it still gives that error. I found that `Set objHTTP = New MSXML2.XMLHTTP60` will work, but it still fails at the `Print objHTTP.Status` with 'Method not valid without suitable object.' – Chloe Oct 23 '13 at 23:05
14

I had to use Debug.print instead of Print, which works in the Immediate window.

Sub SendEmail()
    'Dim objHTTP As New MSXML2.XMLHTTP
    'Set objHTTP = New MSXML2.XMLHTTP60
    'Dim objHTTP As New MSXML2.XMLHTTP60
    Dim objHTTP As New WinHttp.WinHttpRequest
    'Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    'Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URL = "http://localhost:8888/rest/mail/send"
    objHTTP.Open "POST", URL, False
    objHTTP.setRequestHeader "Content-Type", "application/json"
    objHTTP.send ("{""key"":null,""from"":""me@me.com"",""to"":null,""cc"":null,""bcc"":null,""date"":null,""subject"":""My Subject"",""body"":null,""attachments"":null}")
    Debug.Print objHTTP.Status
    Debug.Print objHTTP.ResponseText

End Sub
Chloe
  • 25,162
  • 40
  • 190
  • 357
12

Check out this one:

https://github.com/VBA-tools/VBA-Web

It's a high level library for dealing with REST. It's OOP, works with JSON, but also works with any other format.

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
przemo_li
  • 3,932
  • 4
  • 35
  • 60
0

For reading REST data, at least OData Consider Microsoft Power Query. You won't be able to write data. However, you can read data very well.

Justin Dearing
  • 14,270
  • 22
  • 88
  • 161