XmlHttp Post in Excel VBA not updating website form

核能气质少年 提交于 2019-11-30 23:12:53

Try below code

Public Sub openWebsite(strOpenMethod As String, strURL As String, Optional strPostData As String)

    Dim pXmlHttp As Object
    Set pXmlHttp = CreateObject("MSXML2.XMLHTTP")
    pXmlHttp.Open strOpenMethod, strURL, False
    pXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    pXmlHttp.send (strPostData)


    Dim pHtmlObj As Object
    Set pHtmlObj = CreateObject("htmlfile")
    pHtmlObj.body.innerHTML = pXmlHttp.ResponseText
    MsgBox pXmlHttp.ResponseText

End Sub

Sub test()
    Dim btnSearch As String, strSearchType As String, strSearchName As String, PostData As String
    btnSearch = "Search"
    strSearchType = "Owner"
    strSearchName = "Santosh"
    PostData = "ctl04%24txtOwner=" & strSearchName & "&ctl04%24btnSearch=" & btnSearch & "&ctl04%24rblSearchType=" & strSearchType
    openWebsite "POST", "https://nevadatreasurer.gov/UPSearch/Index.aspx", PostData
End Sub

Post Data view using Firebug

URL encode

ResponeText

Andy G

You should urlencode the characters in this string:

"ctl04$txtOwner=" & strSearchName

Ways to do this are discussed here: SO link, as VBA doesn't have a built-in function for this.

The dollar sign needs to be replaced with %24 and any spaces with %20. If these are the only non-alphanumeric characters in the string they you could take a simple approach, using VBA.Replace() (twice). You are currently replacing spaces with '+' which will usually work, but the dollar-sign may be an issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!