Excel VBA:获取HTML表td的内部文本

时间:2013-11-25 23:14:54

标签: javascript html excel vba excel-vba

我正在使用excel从网页获取值。在其他元素中,HTML包含下表:

<div id="myDiv">    
<table class="myTable">
        <tbody>
            <tr>
                <td>Text1:</td>
                <td class="data"><strong>0.51</strong></td>
            </tr>
            <tr>
                <td>Text2:</td>
                <td class="data"><strong>2199</strong></td>
            </tr>
        </tbody>
    </table>
</div>

页面存储在变量oHtml中。它可以很好地抓住这个表外的其他元素。但是当我尝试捕获值0,51时,我在控制台中使用JS:

document.getElementById("myDiv").getElementsByClassName("myTable")[0].getElementsByClassName("data")[0].innerText

选择值0,51。

但是,函数内使用的以下VBA代码返回#VALUE!

Function myFunction(id)
Call myConnection(id)    
Set myDadta = oHtml.getElementById("myDiv").getElementsByClassName("myTable")(0).getElementsByClassName("data")(0)
        myFunction = myData.innerText
End Function

这是与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

正如我所说,这种语法在同一页面中与此表中的其他元素一起正常工作,为什么我会收到此错误?

2 个答案:

答案 0 :(得分:8)

您的变量名称似乎有拼写错误。您使用过Option Explicit吗?

Function myFunction(id)
    Call myConnection(id)    
    Set myDadta = oHtml.getElementById("myDiv").getElementsByClassName("myTable")(0).getElementsByClassName("data")(0)
    myFunction = myData.innerText   ' <-- This line
End Function

<强>更新

我在VBA表单上放了一个Button,以及以下更正的代码:

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 ' <-- will give 0.51
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://localhost/Test/Test.htm", False   '<-- this is my local machine; replace appropriately
        .send
        oHtml.body.innerHTML = .responseText
    End With
End Sub

在现场网址上展示功能的更新代码

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

screenshot of working demo

答案 1 :(得分:4)

这对我有用。我已设置Microsoft HTML Object Library

的引用

顺便说一句,你错过了"

中的<div id=myDiv">
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

<强>截图

enter image description here