VBA-从ASP页面检索数据

时间:2018-07-10 22:12:27

标签: vba excel-vba automation

我最初创建了一个VBA宏,该宏将使用IE自动化来单击ASPX页面上的特定表单按钮。该页面具有5个您必须通过的表单选项。

  • 组的下拉列表
  • 位置下拉列表
  • 选择日期的图形日历
  • 区域列表框
  • “生成报告”按钮

我想从使用IE自动化转移到更高效的方法。我看过其他文章,他们可以使用MSXML对象提取数据。这是我从阅读中学到的内容,但是没有办法弄清楚如何传递多个表单选项。这是公司特定的/内部的网站,因此很遗憾,我无法对外发布链接示例。

元素ID如下; dlDivision,dlLocation,calRptDate,lbAreas,btnGenReport。

Public Sub XMLhttp_Search_Extract()
Dim URL As String
Dim XMLreq As Object
Dim POSTdata As String
Dim i As Integer

URL = "somewebURL.test.aspx"

POSTdata = "(" & Q("dlDivision") & ":" & Q("divisionNAMEHERE") & "," & Q("dlLocation") & ":" & Q("123 - Location") & ")"

Set XMLreq = CreateObject("MSXML2.XMLHTTP")
With XMLreq
    .Open "POST", URL, False
    .setRequestHeader "User-Agent", "Moilla/5.0 (Windows NT 5.1; rv:23.0) Gecko/20100101 Firefox/23.0"
    .setRequestHeader "Referer", "somewebURL.test.aspx"
    .setRequestHeader "Content-Type", "application/json; charset=utf-8"
    .Send (POSTdata)

    For i = 1 To Len(.responseText) Step 1023
        MsgBox Mid(.responseText, i, i + 1023), _
            Title:=i & " to " & Min(i + 1023 - 1, Len(.responseText)) & " of " & Len(.responseText)
    Next i
End With
End Sub

Private Function Q(text As String) As String
    Q = Chr(34) & text & Chr(34)
End Function

Private Function Min(n1 As Long, n2 As Long) As Long
    Min = IIf(n1 < n2, n1, n2)
End Function

0 个答案:

没有答案