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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Just because it's the same URL doesn't mean the HTML is the same.

The HTML could even change depending on when and how you access the page.

Perhaps there's more than one option element with 2222222 for the value attribute?
 
Upvote 0
Just because it's the same URL doesn't mean the HTML is the same.

The HTML could even change depending on when and how you access the page.

Perhaps there's more than one option element with 2222222 for the value attribute?


Can you expand on this a bit further pls? How is it possible for the html that is retreived by the same method, through excel, to be different in 2 different cases?

each option element has a different value
 
Upvote 0
Well I can't really because I don't know what method you are using to get the HTML.

I was thinking that different browsers could render the pages slightly differently, but that really depends on the type of page it is you are working with.

Admittedly that is more likely to happen with things like fonts etc but you never know.:)
 
Upvote 0
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.
First, it's difficult to help without knowing the URL.

Has the web page completely finished loading? Even though IE.ReadyState = 4 (complete) and IE.Document.readyState = "complete", the page may not have finished loading. Sometimes you have to check for the existence of a specific element to determine if the page is complete.

Try just outputting all the option values and see if they are the same on both computers:

Code:
For Each temp In html.getElementsByTagName("option")         
    debug.print temp.getAttribute("value") 
Next temp
 
Upvote 0
neeel

You didn't say what you were using.:)

What is it you want to do anyway, and what type of page is it?

Have you tried other methods to get a reference to the element you want?

eg GetElementByID
 
Upvote 0
First, it's difficult to help without knowing the URL.

Has the web page completely finished loading? Even though IE.ReadyState = 4 (complete) and IE.Document.readyState = "complete", the page may not have finished loading. Sometimes you have to check for the existence of a specific element to determine if the page is complete.

Try just outputting all the option values and see if they are the same on both computers:

Code:
For Each temp In html.getElementsByTagName("option")         
    debug.print temp.getAttribute("value") 
Next temp

I think the webpage has finished loading, how would I check if it hasnt?. When i was testing to see why it wasnt working, It output ""( or " ", cant remember which) when i did debug.print temp.getattribute("value"), whereas on my pc it outputs the correct values. When I told it to output .innertext, it output the correct text on both pcs.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
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