views:

4495

answers:

4

The code below works. But if I comment out the line "Dim objRequest As MSXML2.XMLHTTP" and uncomment the line "Dim objRequest As Object" it fails with the error message "the parameter is incorrect". Why, and what (if anything) can I do about it?

Public Function GetSessionId(strApiId, strUserName, strPassword) As String

Dim strPostData As String

Dim objRequest As MSXML2.XMLHTTP
'Dim objRequest As Object '

strPostData = "api_id=" & strApiId & "&user=" & strUserName & "&password=" & strPassword

Set objRequest = New MSXML2.XMLHTTP
With objRequest
    .Open "POST", "https://api.clickatell.com/http/auth", False
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .send strPostData
    GetSessionId = .responseText
End With

End Function

A: 

If you use the Dim objRequest As Object then you would need to code:
Set objRequest = CreateObject("MSXML2.XMLHTTP")

Corey Trager
You tell by issuing "New MSXML2.XMLHTTP". For me both ways to do it work.
Tomalak
A: 

Corey, yes, I know I would have to do that in order for my code to work without a reference to the MSXML type library. That's not the issue here. The code fails when using Dim objRequest As Object regardless of whether I use Set objRequest = NEW MSXML2.XMLHTTP with the reference, or Set objRequest = CreateObject("MSXML2.XMLHTTP") without the reference.

Brendan Reynolds
+3  A: 

For some reason, this works:

Dim strPostData As String
Dim objRequest As Object

strPostData = "api_id=" & strApiId & "&user=" & strUserName & "&password=" & strPassword

Set objRequest = New MSXML2.XMLHTTP
With objRequest
  .Open "POST", "https://api.clickatell.com/http/auth", False
  .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  .send (strPostData)
   GetSessionId = .responseText
End With
Tomalak
Yay! Thank you, Tomalak, that fixed it! :)
Brendan Reynolds
Very strange that the solution was to add parathensis to `.send (strPostData)`. But I just confirmed that it is really the case.
Bruno Rothgiesser
@Bruno: Thanks for the confirmation. I'm convinced that there is a completely logical explanation for this, somewhere in the guts of VBA or VBA-to-COM interaction in general or the MSXML2.XMLHTTP library in particular, but I have no idea. I'm sure @Eric Lippert could explain. ;)
Tomalak
A: 

I realise this is nearly identical to the code from Tomalek above (all credit due to you!), but this question helped me towards a full solution to a problem I had (Excel submitting to PHP server, then dealing with response)...so in case this is of any help to anyone else:

Sub Button1_Click2()

Dim objXMLSendDoc As Object
Set objXMLSendDoc = New MSXML2.DOMDocument
objXMLSendDoc.async = False
Dim myxml As String
myxml = "<?xml version='1.0'?><Request>Do Something</Request>"
If Not objXMLSendDoc.LoadXML(myxml) Then
    Err.Raise objXMLSendDoc.parseError.ErrorCode, , objXMLSendDoc.parseError.reason
End If

Dim objRequest As MSXML2.XMLHTTP
Set objRequest = New MSXML2.XMLHTTP
With objRequest
    .Open "POST", "http://localhost/SISADraftCalcs/Test2.php", False
    .setRequestHeader "Content-Type", "application/xml;charset=UTF-16"
    .setRequestHeader "Cache-Control", "no-cache"
    .send objXMLSendDoc
End With

Dim objXMLDoc As MSXML2.DOMDocument
Set objXMLDoc = objRequest.responseXML
If objXMLDoc.XML = "" Then
    objXMLDoc.LoadXML objRequest.responseText
    If objXMLDoc.parseError.ErrorCode <> 0 Then
        MsgBox objXMLDoc.parseError.reason
    End If
End If

Dim rootNode As IXMLDOMElement
Set rootNode = objXMLDoc.DocumentElement

MsgBox rootNode.SelectNodes("text").Item(0).text

End Sub
Alistair Collins