可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm using excel to get values from a webpage. Among other elements, the HTML contains the following table:
The page is stored in variable oHtml. It is working fine to grab other elements outside this table. But when I try to capture the value 0,51 for example, I use JS in the console:
document.getElementById("myDiv").getElementsByClassName("myTable")[0].getElementsByClassName("data")[0].innerText
And the value 0,51 is selected.
However, the following VBA code used inside a function is returning #VALUE!
Function myFunction(id) Call myConnection(id) Set myDadta = oHtml.getElementById("myDiv").getElementsByClassName("myTable")(0).getElementsByClassName("data")(0) myFunction = myData.innerText End Function
This is the connection to IE:
Public Sub myConnection(id) Set oHtml = New HTMLDocument With CreateObject("WINHTTP.WinHTTPRequest.5.1") .Open "GET", "http://www.example.com" & id, False .send oHtml.body.innerHTML = .responseText End With End Sub
As I said, this kind of syntax is working fine with other elements out of this table in this same page, so why am I getting this error?
回答1:
You seem to have a typo in your variable name. Have you used Option Explicit
?
Function myFunction(id) Call myConnection(id) Set myDadta = oHtml.getElementById("myDiv").getElementsByClassName("myTable")(0).getElementsByClassName("data")(0) myFunction = myData.innerText '
UPDATE
I put a Button on VBA form and the following corrected code that works:
Option Explicit Dim oHtml, myData Private Sub CommandButton1_Click() MsgBox myFunction(0) End Sub Function myFunction(id) Call myConnection(id) Set myData = oHtml.getElementById("myDiv").getElementsByTagName("Table")(0).getElementsByTagName("td")(1) myFunction = myData.innerText '
UPDATED CODE TO DEMONSTRATE FUNCTION ON LIVE URL
Option Explicit Dim oHtml, myData Private Sub CommandButton1_Click() MsgBox myFunction(0) End Sub Function myFunction(id) Call myConnection(id) Set myData = oHtml.getElementById("overallRatios").getElementsByTagName("Table")(0).getElementsByTagName("td")(1) myFunction = myData.innerText End Function Public Sub myConnection(id) Set oHtml = New HTMLDocument With CreateObject("WINHTTP.WinHTTPRequest.5.1") '.Open "GET", "http://www.example.com" & id, False .Open "GET", "http://www.reuters.com/finance/stocks/overview?symbol=PTI.LS", False .send oHtml.body.innerHTML = .responseText End With End Sub
回答2:
This works for me. I have set reference to Microsoft HTML Object Library
BTW you are missing a "
in Option Explicit Sub Sample() Dim objIe As Object, xobj As HTMLDivElement Set objIe = CreateObject("InternetExplorer.Application") objIe.Visible = True objIe.navigate "C:\a.htm" While (objIe.Busy Or objIe.READYSTATE 4): DoEvents: Wend Set xobj = objIe.document.getElementById("myDiv") Set xobj = xobj.getElementsByClassName("myTable").Item(0) Set xobj = xobj.getElementsByClassName("data")(0) Debug.Print xobj.innerText Set xobj = Nothing objIe.Quit Set objIe = Nothing End Sub
Screenshot: