7

I'm using excel to get values from a webpage. Among other elements, the HTML contains the following table:

<div id="myDiv">    
<table class="myTable">
        

2 Answers2

9

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   ' <-- This line
End Function

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 ' <-- 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

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

screenshot of working demo

Pradeep Kumar
  • 6,836
  • 4
  • 21
  • 47
  • + 1 LOL Nice catch :) – Siddharth Rout Nov 26 '13 at 11:11
  • 1
    That was just at the first glance. There are more problems with the code. Posting updates... – Pradeep Kumar Nov 26 '13 at 11:40
  • Sorry, it returns #VALUE! Can you get the value of the Beta of this page (1.12) http://www.reuters.com/finance/stocks/overview?symbol=PTI.LS –  Nov 26 '13 at 12:16
  • Bingo! It works, finnally! Now the whole sheet is too slow, but that's another question, if you have any clues, I'd appreciate. Thanks a lot for your help! –  Nov 26 '13 at 12:58
  • 2
    Good work pradeep :) Wish i could upvote this again. @NunoNogueira: If this is what you were looking for then accept this answer rather than mine ;) – Siddharth Rout Nov 26 '13 at 13:06
  • Yes, it was an excellent work, if you don't mind, I'll accept his answer then :-D –  Nov 26 '13 at 13:07
  • 2
    @NunoNogueira: AFAIK Sid, he never answers questions for points. He has a pet phrase something which says that points can't buy you harley davidson. However I feel that he had answered what your actual quesiton was. My answer is just an update to your edit. BTW avoid using `getElementsByClassName` as it won't work on all IE versions and all browsers consistantly. Or you will have to define your own javascript extension methods explicitly. – Pradeep Kumar Nov 26 '13 at 13:27
  • 1
    @PradeepKumar: Let's stop confusing the poor guy about what to accept and what not to :) He has made a choice which is right in my opinion and lets stick to it... Oh you remember the phrase :P – Siddharth Rout Nov 26 '13 at 13:32
  • 1
    @SiddharthRout: Yes I remember it from the MVP Summit in Goa ;) – Pradeep Kumar Nov 26 '13 at 13:34
  • 2
    @NunoNogueira: Regarding the slowness, it is because everything is working synchronously and on single thread. Excel VBA is basically single threaded. Had it been .NET or any other language that supports multi-threading, you could have used that capability to speed it up. The best you can do is try putting `DoEvents` somewhere inside the loop to stop the UI from freezing. – Pradeep Kumar Nov 26 '13 at 13:38
  • 1
    On this line `.Open "GET", "the url", False`, you can pass `True` instead of `False` to make it asynchronous. But that will involve more work to make the entire thing to get working :) – Pradeep Kumar Nov 26 '13 at 14:15
  • Great! I'll check that. I tried to push the pages with a timestamp and the symbol to an array, so that the UDF would call a kind of "cached" pages if they existed to make things faster. The array worked fine but somehow the process of checking existing pages for every UDF request made things very complicated. I'll check if .NET may be integrated with Excel. –  Nov 26 '13 at 14:21
3

This works for me. I have set reference to Microsoft HTML Object Library

BTW you are missing a "in <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

Screenshot:

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • I get Run-time error '91' Object variable or With block variable not set on line Set xobj ... –  Nov 26 '13 at 10:02
  • Did you set a reference to `Microsoft HTML Object Library`? – Siddharth Rout Nov 26 '13 at 10:04
  • Yes, your code is correct. I can now see, but it doesn't work for me because I established a different connection, I'll edit my question above. –  Nov 26 '13 at 10:20
  • Well thanks for helping anyway, I'll accept your answer since the code is good. –  Nov 26 '13 at 11:47
  • I hadn't seen the update. I'll check. The code in the function is opening a web connection making the application so slow I don't think it will be viable like this. Anyway, I'll give feedback, thanks! –  Nov 26 '13 at 12:05