user1140067
user1140067

Reputation:

automate submitting a post form that is on a website with vba and xmlhttp

I'm using xmlhttp via vba in excel 2010. I need to programmatically add an item to a shopping cart on a website. I have the code below so far, it uses the POST method

A couple of things I think are wrong with my code but not sure how to fix - It doesn't show where the form is that is being submitted. Here is that url:

http://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx

The url I entered as the url that processes the form is the url in the "action=" part of "form".

How can I verify that the form posted?

Sub post_frm()
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
' Indicate that page that will receive the request and the
' type of request being submitted
xmlhttp.Open "POST", "http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge", False
' Indicate that the body of the request contains form data
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
' Send the data as name/value pairs
xmlhttp.send "Quantity=1&VariantID=2705&ProductID=2688"
Set xmlhttp = Nothing
End Sub

Upvotes: 5

Views: 14889

Answers (2)

ashleedawg
ashleedawg

Reputation: 21619

A variation of the other answer works for me without the need for IE.

Sub Post_HTTP_Form()
'Requires reference to "Microsoft XML, v6.0" or better. (Tools>References)
    Dim msXML As New XMLHTTP60, resp As String
    With msXML
        .Open "POST", "{http://YOUR_URL_HERE.com}", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .Send "{PARAMETER}={VALUE}" 
        resp = StrConv(.responseBody, vbUnicode)
    End With
    Debug.Print resp 'outputs to Immediate Window (CTRL+G to view)
    Set msXML = Nothing
End Sub

Just replace the three values in {curly braces}.


...and a late-bound version:

Sub Post_HTTP_Form()
    Dim msXML As Object, resp As String
    Set msXML = CreateObject("MSXML2.ServerXMLHTTP")
    With msXML
        .Open "POST", "{http://YOUR_URL_HERE.com}", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .Send "{PARAMETER}={VALUE}" 
        resp = StrConv(.responseBody, vbUnicode)
    End With
    Debug.Print resp 'outputs to Immediate Window (CTRL+G to view)
    Set msXML = Nothing
End Sub

Upvotes: 1

Siddharth Rout
Siddharth Rout

Reputation: 126

There is nothing wrong with the code. :) I tested it and it works fine. The error might be somewhere else.

I just tweaked the code little bit to use IE to test the output and it works just fine now :) I have tested it in Excel 2007 at the moment. Will test it in 2010 shortly. BTW which version of IE are you using?

Here is the code that I tested and it works just fine.

Option Explicit

Sub post_frm()

    Dim objIE As Object, xmlhttp As Object
    Dim response As String

    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.navigate "about:blank"
    objIE.Visible = True

    Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")

    '~~> Indicates that page that will receive the request and the type of request being submitted
    xmlhttp.Open "POST", "http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge", False
    '~~> Indicate that the body of the request contains form data
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    '~~> Send the data as name/value pairs
    xmlhttp.Send "Quantity=1&VariantID=2705&ProductID=2688"

    response = xmlhttp.responseText
    objIE.document.Write response

    Set xmlhttp = Nothing

End Sub

Regards

Sid

Upvotes: 11

Related Questions