尝试进行Web抓取时VBA运行时91错误

时间:2020-03-01 00:21:29

标签: excel vba web-scraping

我正在尝试通过VBA自动化一些Web上的数据抓取,并且始终收到

运行时错误91“未设置对象变量”

在线

For Each htmlele In ieobj.document.getElementById("j_id_jsp_747894496_2").getElementsByTagName("tr")

我在下面提供了完整的代码。我可以看到没有声明htmlele,但是我不确定这可能是问题所在。所有必需的对象库都已加载,因此VBA识别对象没有问题,但不确定为什么会出错。

Sub import_ECS()
    Dim courses As Workbook
    Set courses = Excel.Workbooks("CANTRAC Courses")
    Dim ECSws As Worksheet
    Set ECSws = courses.Sheets("ECS")
    Dim ieobj As InternetExplorer
    Set ieobj = New InternetExplorerMedium

    Dim htmlele As IHTMLElement

    Dim i As Integer
    i = 2

    Dim ECSURL As String
    ECSURL = "https://app.prod.cetars.training.navy.mil/cantrac/pages/rpt_classes_by_cin.html?cin=A-830-0030&p=1"


    ieobj.navigate ECSURL
    Application.Wait Now + TimeValue("00:00:15")



    For Each htmlele In ieobj.document.getElementById("j_id_jsp_747894496_2").getElementsByTagName("tr")
        With ECSws
            .Range("A" & i).Value = htmlele.Children(0).textContent
            .Range("B" & i).Value = htmlele.Children(1).textContent
            .Range("C" & i).Value = htmlele.Children(2).textContent
            .Range("D" & i).Value = htmlele.Children(3).textContent
            .Range("E" & i).Value = htmlele.Children(4).textContent
            .Range("F" & i).Value = htmlele.Children(7).textContent
            .Range("G" & i).Value = htmlele.Children(8).textContent
            .Range("H" & i).Value = htmlele.Children(9).textContent
            .Range("I" & i).Value = htmlele.Children(10).textContent
        End With
    i = i + 2
    Next htmlele

End Sub

0 个答案:

没有答案