Discussion:
Using MSXML2.XMLHTTP in VBA?
(too old to reply)
Kevin07003
17 years ago
Permalink
Hello,
I'm trying to use the XMLHTTP object from the MSXML2 library in Outlook VBA.
I get a compile error "Expected: =" on the "oReq.Open" command.

There may be a better way to send an HTTP Request and get the XML response
from VBA. If so, please let me know...

Here is my code:
Private Sub TestXMLHTTP()
Dim strURL As String
Dim oReq As XMLHTTP

On Error GoTo ErrRoutine

strURL =
"http://local.yahooapis.com/MapsService/V1/geocode?appid=YahooDemo&street=701+First+Ave&city=Sunnyvale&state=CA"
MsgBox (strURL)
oReq.Open("GET", strURL, False)
oReq.Send
Debug.Print oReq.responseText
Set oReq = Nothing
MsgBox (oReq.responseText)

EndRoutine:
Exit Sub

ErrRoutine:
MsgBox Err.Number & " - " & Err.Description, _
vbOKOnly Or vbCritical, _
"TestXMLHTTP"
GoTo EndRoutine

End Sub

Thanks for your help!

Kevin
Ken Slovak - [MVP - Outlook]
17 years ago
Permalink
I have no idea what this has to do with Outlook programming, but in VBA code
try either of these two lines:

Call oReq.Open("GET", strURL, False)

or

oReq.Open "GET", strURL, False

You only use the parenthesis for a function call with a return value or when
using the Call keyword.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
...
Kevin07003
17 years ago
Permalink
Thanks - that was part of the problem. I haven't used VBA since 1998 or so!

Here is the working code that also parses the XML Response:
Private Sub TestXMLHTTP()
Dim strURL As String
Dim oReq As Object
Set oReq = CreateObject("MSXML2.XMLHTTP")
Dim oDOM As Object
Set oDOM = CreateObject("MSXML2.DOMDocument.3.0")
oDOM.async = "false"
Dim oNodeList As IXMLDOMNodeList

Dim strLat, strLon As String

On Error GoTo ErrRoutine


strURL =
"http://local.yahooapis.com/MapsService/V1/geocode?appid=YahooDemo&street=701+First+Ave&city=Sunnyvale&state=CA"
oReq.Open "GET", strURL, False
oReq.Send
oDOM.LoadXML (oReq.responseText)
Set oReq = Nothing

Set oNodeList = oDOM.getElementsByTagName("Latitude")
If oNodeList.Length = 0 Then
MsgBox ("Latitude not found!")
GoTo EndRoutine
End If
strLat = oNodeList.Item(0).Text

Set oNodeList = oDOM.getElementsByTagName("Longitude")
If oNodeList.Length = 0 Then
MsgBox ("Longitude not found!")
GoTo EndRoutine
End If
strLon = oNodeList.Item(0).Text

MsgBox ("Latitude: " & strLat & Chr$(13) & "Longitude :" & strLon)


EndRoutine:
Exit Sub

ErrRoutine:
MsgBox Err.Number & " - " & Err.Description, _
vbOKOnly Or vbCritical, _
"TestXMLHTTP"
GoTo EndRoutine
End Sub
...
Loading...