The send method MSXML2.XMLHTTP works with early binding, failing with late binding

Below is the code. But if I comment out the Dim objRequest As MSXML2.XMLHTTP line and uncomment the Dim objRequest As Object , it will end with an error message:

Invalid parameter

Why and what (if anything) can I do with this?

 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 

Corey, yes, I know that I will need to do this so that my code works without reference to the MSXML type library. It's not a problem. Code doesn't work when using Dim objRequest As Object no matter if I use

Set objRequest = NEW MSXML2.XMLHTTP with reference, or

Set objRequest = CreateObject("MSXML2.XMLHTTP") without a link.

+7
source share
3 answers

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 

Instead of creating a URL-encoded strPostData using string concatenation, it is strongly recommended that you use the URL encoding function:

 strPostData = "api_id=" & URLEncode(strApiId) & _ "&user=" & URLEncode(strUserName) & _ "&password=" & URLEncode(strPassword) 

Several options for the URLEncode() function in VBA are in this thread: How can I encode a URL in Excel VBA?

+14
source

If you are using a Dim objRequest As object, then you will need to enter the code:
Set objRequest = CreateObject ("MSXML2.XMLHTTP")

+2
source

I understand that this is almost identical to the code from Tomalek above (all the credits related to you!), But this question helped me completely solve the problem that I encountered (Excel is sent to the PHP server and then deals with the answer) .. therefore, in case this can help 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 
0
source

All Articles