VBA 函数不返回 HTMLElementCollection 对象
VBA Function not returning HTMLElementCollection object
我正在尝试 return 从函数中获取 HTMLElementCollection。然而,函数方面的一切都正常工作,但是当代码 returns 到调用子时,分配给函数输出的变量 "myTable" 显示“<无变量>”。我尝试将集合作为 scripting.dictionary 的一部分传回,但结果相同。
感谢任何帮助。谢谢大家
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
经过一些测试,我想我知道发生了什么...尝试在主子 (updateReports) 中创建 HTMLDocument 并通过 byRef 参数将其传递给 getTable 函数。同时将函数的返回类型更改为 HTMLElementCollection。类似的东西:
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
让我知道这是否适合您!
我正在尝试 return 从函数中获取 HTMLElementCollection。然而,函数方面的一切都正常工作,但是当代码 returns 到调用子时,分配给函数输出的变量 "myTable" 显示“<无变量>”。我尝试将集合作为 scripting.dictionary 的一部分传回,但结果相同。
感谢任何帮助。谢谢大家
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
经过一些测试,我想我知道发生了什么...尝试在主子 (updateReports) 中创建 HTMLDocument 并通过 byRef 参数将其传递给 getTable 函数。同时将函数的返回类型更改为 HTMLElementCollection。类似的东西:
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
让我知道这是否适合您!