如何从VBA Web表单查询中获取/提取结果集

时间:2016-06-14 16:40:33

标签: vba excel-vba excel

我使用以下VBA提交网络表单。查询工作得很好,但我不知道如何捕获查询的结果。我正在运行Excel中的查询,因此最好将结果显示在消息msgbox(“Dispay Result”)或Cell中。我尝试使用IE对象(IE.text,IE.value),但没有运气。

Dim IE As Object

Sub GetOFAC()

Application.ScreenUpdating = False

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "https://sanctionssearch.ofac.treas.gov/"

    Application.StatusBar = "Submitting"
    ' Wait while IE loading...
    While IE.Busy
        DoEvents
    Wend
    ' **********************************************************************
    delay 5
    IE.Document.getElementById("ctl00_MainContent_txtLastName").Value = "Apple"
    delay 5
    IE.Document.getElementById("ctl00_MainContent_btnSearch").Click
    '**********************************************************************
    Application.StatusBar = "Form Submitted"
    IE.Quit
    Set IE = Nothing

1 个答案:

答案 0 :(得分:1)

详细说明我的评论。您似乎并未尝试在任何地方获取结果。结果列在ID为.GetElementsbyID的表中。您可以使用.InnerText方法从DOM中选择此表元素,然后可以使用Dim IE As Object Sub GetOFAC() Dim strResults as String Application.ScreenUpdating = False Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True IE.Navigate "https://sanctionssearch.ofac.treas.gov/" Application.StatusBar = "Submitting" ' Wait while IE loading... While IE.Busy DoEvents Wend ' ********************************************************************** delay 5 IE.Document.getElementById("ctl00_MainContent_txtLastName").Value = "Apple" delay 5 IE.Document.getElementById("ctl00_MainContent_btnSearch").Click '********************************************************************** Application.StatusBar = "Form Submitted" ' Wait while the results load While IE.Busy DoEvents Wend 'The results have loaded so select the table and get the text strResults = IE.Document.getElementById("gvSearchResults").InnerText MsgBox strResults IE.Quit Set IE = Nothing 属性查看此元素中的文本。

target <- c('1', '1', '1', '1', '2', '2', '2', '3', '3', '3', '4', '4', '4', '4', '4', '5', '5', '5', '5')

source <- c('2', '3', '4', '5', '1', '3', '4', '2', '3', '4', '1', '2', '3', '4', '5', '1', '2', '4', '5')

value <- c(1, 2, 2, 1, 1, 1, 4, 1, 2, 1, 1, 1, 1, 2, 2, 1, 2, 2, 2)

我还没有对此进行测试,但它应该让您更好地了解如何与IE和DOM(文档对象模型)进行交互。