help with html extraction in vba

neeeel

New Member
Joined
Jun 24, 2011
Messages
8
there is a bit of code I have written, which works on my pc, but doesnt work on someone elses. I am really confused.the code in question is
Code:
[COLOR=blue]Dim[/COLOR] temp [COLOR=blue]As[/COLOR] HtmlHtmlElement  [COLOR=blue] 
Dim[/COLOR] s [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]   
s = "2222222"   
[COLOR=blue]For Each[/COLOR] temp [COLOR=blue]In[/COLOR] html.getElementsByTagName("option")      [COLOR=blue]   
    If[/COLOR] temp.getAttribute("value") = s [COLOR=blue]Then[/COLOR]             
    r.Offset(0, 1) = (temp.innerText)               [COLOR=blue]   
    End[/COLOR] [COLOR=blue]If[/COLOR]  [COLOR=blue] 
Next[/COLOR] temp
r is a range object that is passed to the sub.
the variable html is an object that has been loaded with html from a webpage.

This code works fine on my pc, it finds the "option" tags in the html source , and then checks to see if the "value" attribute is equal to the string s. When I run it on someone elses pc , temp.getAttribute("value") returns a blank string, even though there is an attribute called value. The web page address is hard coded so its not that hes using the wrong URL
I use excel 2007, he uses 2010

Anyone got any ideas?
thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I think the webpage has finished loading, how would I check if it hasnt?
Or more importantly, this will wait until it has finished loading:
Code:
     Set html = New HTMLDocument
    html.body.innerHTML = XML.responseText
    Do While html.readyState <> "complete"
        DoEvents
        Application.Wait Now + TimeValue("00:00:01")
    Loop
You might need to include a timeout in the loop to prevent it waiting forever if readyState is never "complete".

If this doesn't work you may have to do what I first suggested and check for the existence of a specific HTML element.
 
Upvote 0
Firstly, did the readyState loop solve the problem?

Try something like this to wait for the existence of a specific HTML element (also shows the loop timeout I mentioned):
Code:
Sub Wait_for_HTML_element()

    Dim html As HTMLDocument
    Dim elem As HTMLGenericElement
    Dim timeout As Date, t As Single
    
    timeout = Now + TimeValue("00:00:30")
    Set elem = Nothing
    While Now < timeout And elem Is Nothing
        Set elem = Get_Element(html)
        If elem Is Nothing Then t = Timer: While Timer - t < 0.25: Wend     'wait 0.25 seconds
        DoEvents
    Wend
                    
    If elem Is Nothing Then
        Debug.Print "Element not found"
    Else
        Debug.Print "Element found"
    End If

End Sub


Private Function Get_Element(HTMLdoc As HTMLDocument) As HTMLGenericElement
    
    Dim elems As IHTMLElementCollection
    Dim thisElem As HTMLGenericElement
    Dim i As Integer
    
    Set elems = HTMLdoc.getElementsByTagName("xxx")
    Set Get_Element = Nothing
    i = 0
    While i < elems.Length And Get_Element Is Nothing
        Set thisElem = elems(i)
        If thisElem.nodeName = "yyy" And thisElem.nodeValue = "zzz" Then Set Get_Element = thisElem
        i = i + 1
    Wend
    
End Function
The Sub shows the general algorithm. The Get_Element function would be coded for your specific web page, and may not necessarily use the techniques shown. For instance, you might use getElementsById; or a specific HTML element like HTMLtableCell rather than HTMLGenericElement; or you might loop through the HTMLDocument.all collection; or look at a specific element property rather than nodeName, etc., etc. It all depends on your specific web page and your specific requirement.
 
Upvote 0
thanks for your help.
Im not clear on when I would use this, after I have done the .send command to get the page, or after I have checked the ready state? or where?

At least I have a couple of things to try out, although I am sceptical that its due to a not fully loaded page

thanks
 
Upvote 0
Just passing.

To check an XMLHTTP request has completed you need to do something like this.
Code:
        Set oXMLHTTP = New XMLHTTP
 
        oXMLHTTP.Open "GET", strURL, False
 
        oXMLHTTP.send
 
        'Wait for request to finish
        Do While oXMLHTTP.readyState <> 4
            DoEvents
        Loop
You can also check the status using either the status or statusText property, for example:
Code:
If oXMLHTTP.status = 200 Then
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top