XmlHttp在Excel VBA中发布不更新网站表单

时间:2013-07-23 19:04:35

标签: vba excel-vba xmlhttprequest excel-2010 msxml

我经常需要在NV的状态中搜索无人认领的属性,并将结果放在Excel电子表格中。我正在尝试自动化该过程,但我仅限于使用Excel 2010和VBA。以下是我尝试使用xmlhttp提交表单的网站的URL。

网址:https://nevadatreasurer.gov/UPSearch/

我创建了一个类来自动在其他网站上提交表单,但无论我在postdata中输入什么内容,表单都从未提交过。以下是我的提交和提交表单的方法。

致电课程:

cXML.openWebsite "Post", "https://nevadatreasurer.gov/UPSearch/Index.aspx", _
                 "ctl04$txtOwner=" & strSearchName

班级方法:

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

pXmlHttp.Open strOpenMethod, strURL


If strPostData <> "" Then
    strPostData = convertSpaceToPlus(strPostData)
    pXmlHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    pXmlHttp.send (strPostData)
Else
    pXmlHttp.send
End If

'Create DOM html documnet
pHtmlObj.body.innerHTML = pXmlHttp.responseText

End Sub

每次responseText是没有更新的主网站,就好像我没有提交postdata一样。我是IE自动化的新手,但是有人可以提供一个不起作用的原因和一个有效的代码示例吗?

谢谢!

更新:2013年7月26日上午8:30太平洋标准时间

我的方法没有任何变化,我可以通过其他网站提交表格。 OR无人认领的财产形式。它完美无缺!

网址:https://oregonup.us/upweb/up/UP_search.asp

但是,当我尝试CA无人认领的房产网站时,我遇到了同样的问题。无论我做什么,responseText始终是原始搜索页面,没有更新。

网址:https://scoweb.sco.ca.gov/UCP/Default.aspx

在原始帖子中,它仍然不适用于NV的状态。我正在使用适当的帖子数据,为每个网站编码的URL,并没有看到任何区别。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:6)

尝试以下代码

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

使用Firebug发布数据视图

enter image description here

网址编码

enter image description here

<强> ResponeText

enter image description here

答案 1 :(得分:2)

你应该对这个字符串中的字符进行urlencode:

"ctl04$txtOwner=" & strSearchName

这里讨论了如何做到这一点:SO link,因为VBA没有内置函数。

美元符号需要替换为%24和%20的任何空格。如果这些是字符串中唯一的非字母数字字符,则可以使用VBA.Replace()(两次)采用简单的方法。您目前正在用“+”替换空格,这通常会起作用,但美元符号可能是一个问题。