Excel Macro Extracting Data from Website

joman

New Member
Joined
May 16, 2009
Messages
5
I am currently experiencing a problem pulling some data of a web site using Excel. I have a collection of HTMLInputElement objects from which I extract the one I want. I then set the value (text) of the object and submit the form. Here is my code:
...
Dim inputElements As IHTMLElementCollection
Dim inputCounter As Integer
Dim inputETFIndex As Integer
Do
Dim etfInput As HTMLInputElement
ie.Visible = False

ie.Navigate "http://finance.yahoo.com/etf"
Do
Loop While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE

Set doc = ie.document
Set inputElements = doc.getElementsByTagName("INPUT")

If Counter = 1 Then
For inputCounter = 0 To inputElements.Length - 1
Set inputele = inputElements.Item(inputCounter)
If inputele.Name = "s" And inputele.Size = 7 Then
Set etfInput = inputele
inputETFIndex = inputCounter
Exit For
End If
Next inputCounter
Else
Set etfInput = inputElements.Item(inputETFIndex)
End If

etfInput.Value = ETFSymbol
etfInput.form.submit
...

The problem is the line etfInput.Value = ETFSymbol
where I get the error message: "Object Variable or With Block Variable Not Set".

Any assistance would be greatly appreciated.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
is it because of the 2 DO's?

you never end the loop
 
Upvote 0
Could it be because what you are looking for is never found?

If it isn't then etfSymbol will be Nothing.

Also have you tried finding what you are looking for using another method?

Something like this perhaps.
Rich (BB code):
    Set IE = CreateObject("InternetExplorer.application")
    IE.Visible = True
    IE.Navigate ("http://finance.yahoo.com/etf")
    Do
        If IE.ReadyState = 4 Then
            Exit Do
        Else
            DoEvents
        End If
    Loop
    
    Set doc = IE.Document
    
    Set ctl = doc.getElementById("txtQuotes")
    
    ctl.Value = "ETFSymbol"
    
    doc.Forms(1).submit ' ctl.Form.submit
    IE.Quit
 
Upvote 0
Thanks for your response. When I run the following code, the execution stops on the second line complaining about an object variable or with block variable not set. I have an outer loop that iterates over the different ETFSymbols.

Set doc = ie.document
doc.getElementById("txtQuotes").Value = ETFSymbol
doc.forms(1).submit

When I debug the code, the program execution stops on the second line but I can press F8 and the run time engine does not complain. I tried on error resume next but the that appears to give wierd results.
 
Upvote 0
Well all I can say is the code I posted works for me, and that is all the code.

It's messy, I've not declared any variables, not referenced anything, it should have a loop after submitting the form etc.

And obviously I'm using a bogus symbol, 'EFTSymbol' - so no 'real' results.

How exactly did you try the code?

PS Don't add On Error Resume next, if you are having a problem step through the code and try and figure out what it is.

On Error... can have it's uses but can also hide errors.:)
 
Upvote 0
Hi,
I managed to get it to work by adding a sleep statement before the:
Code:
set doc = ie.document
[\code]
 
I'm not sure if this is the best approach.
 
Perhaps
[code]
Do
Loop While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE
[\code]
is not working properly
 
Upvote 0
Try
Code:
    IE.navigate "http://finance.yahoo.com/etf"       
    While IE.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    While IE.document.readyState <> "complete": DoEvents: Wend        
    Set doc = IE.document
 
Upvote 0
I appears to be working with the above looping statements which wait for internet explorer to finish loading. Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,773
Messages
6,126,820
Members
449,340
Latest member
hpm23

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