2013-11-25 426 views
4

我使用excel從網頁獲取值。 HTML中包含下列表格:Excel VBA:獲取HTML表格的內部文本td

<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 

正如我所說的,這種語法是工作的罰款與此相同頁面的其他元素了這個表的,所以爲什麼我得到這個錯誤?

回答

7

你似乎在你的變量名拼寫錯誤。你用過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 

UPDATE

我把VBA表單上的按鈕和下面的修正代碼工作:

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 

更新的代碼來演示功能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 

screenshot of working demo

+0

+ 1大聲笑很好聽:) –

+1

這只是第一眼。代碼存在更多問題。發佈更新... –

+0

對不起,它返回#VALUE!你能得到這個頁面的Beta值(1.12)http://www.reuters.com/finance/stocks/overview?symbol=PTI.LS –

4

這適用於我。我已經設置參考Microsoft HTML Object Library

BTW你缺少一個"<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

+0

我得到運行時錯誤'91'對象變量或塊變量沒有在線設置設置xobj ... –

+0

您是否設置了對「Microsoft HTML Object Library」的引用? –

+0

是的,你的代碼是正確的。我現在可以看到,但它不適用於我,因爲我建立了不同的連接,我將在上面編輯我的問題。 –