VBA函数未返回HTMLElementCollection对象

时间:2017-04-09 19:56:55

标签: html vba excel-vba function return-value

我试图从函数返回HTMLElementCollection。但是,功能方面的一切都应该工作,但是当代码返回到调用子时,分配给函数输出的变量" myTable"显示"<没有变量>"。我尝试将该集合作为脚本编写的一部分传回,但结果相同。

感谢任何帮助。谢谢大家。

Sub updateReports()
'//Function gathers latest report information and adds to sheets("Reports")
'//URL
Dim strURL As String
    strURL = "http://www.ndbc.noaa.gov/station_page.php?station=62103"

        Dim myTable As HTMLElementCollection
        Set myTable = getTable(strURL)
        '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        'From here on, "myTable" listed as '<No Variables>'.
        'HTMLElementCollection not sucessfully returned.
        '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
End Sub

Public Function getTable(strURL As String) As Variant
'//Downloads HTML Table from strURL
'//Create HTTP Object
Dim oXMLHTTP As Object
    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.6.0")
        oXMLHTTP.Open "GET", strURL, False
        oXMLHTTP.send

Dim HTMLDoc As New HTMLDocument
    If oXMLHTTP.Status = 200 Then
        HTMLDoc.body.innerHTML = oXMLHTTP.responsetext
        Set getTable = HTMLDoc.getElementsByTagName("tr")()
        '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        'Under "getTable" call stack, getTable shows correct object (HTMLElementCollection)
        '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    End If
End Function

1 个答案:

答案 0 :(得分:1)

After some testing, I think I figured it out what's going on... Try creating the HTMLDocument in the main sub (updateReports) and pass it to the getTable function through an byRef Argument. Also change the returning type of the function to HTMLElementCollection. Something like that:

Sub updateReports()
'//Function gathers latest report information and adds to sheets("Reports")
'//URL
Dim strURL As String
    strURL = "http://www.ndbc.noaa.gov/station_page.php?station=62103"

        Dim myTable As HTMLElementCollection
        Dim HTMLDoc As New HTMLDocument

        Set myTable = getTable(strURL, HTMLDoc)
        '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        'From here on, "myTable" listed as '<No Variables>'.
        'HTMLElementCollection not sucessfully returned.
        '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
End Sub

Public Function getTable(strURL As String, ByRef HTMLDoc As HTMLDocument) As HTMLElementCollection
'//Downloads HTML Table from strURL
'//Create HTTP Object
Dim oXMLHTTP As Object
    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.6.0")
        oXMLHTTP.Open "GET", strURL, False
        oXMLHTTP.send

    If oXMLHTTP.Status = 200 Then
        HTMLDoc.body.innerHTML = oXMLHTTP.responsetext
        Set getTable = HTMLDoc.getElementsByTagName("tr")()
        '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        'Under "getTable" call stack, getTable shows correct object (HTMLElementCollection)
        '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    End If
End Function

Let me know if this works out for you!