3

I'm trying to copy the last price of share prices into an Excel worksheet from

<div id="last-price-value" vkey="LastPrice">127.36</div>.

Internet Explorer opens and navigates to the site but the last price isn't copied to my worksheet. I added Debug.Print element.innertext but nothing appears in the immediate pane.

Dim element As IHTMLElement
Dim elements As IHTMLElementCollection

Dim ie As New InternetExplorer
Dim html As HTMLDocument

my_page = "http://www.morningstar.com/stocks/XASX/COH/quote.html"

With ie
    .Visible = TRUE
    .Navigate my_page

    Do While ie.readyState <> READYSTATE_COMPLETE
    Loop

    Set html = ie.document
    Set element = html.getElementById("last-price-value")    
    Debug.Print element.innertext

    Sheets("Sheet 1").Range("A2").Value = element.innertext     
End With
Community
  • 1
  • 1
Edward Murray
  • 31
  • 1
  • 1
  • 2
  • 1
    There is no such thing as an element with ID set to *last-price-value* in the page pointed in your code. You can check it by opening the page with a web browser, open the web tools, go to the console and execute ```document.getElementByID('last-price-value')```, it will return null. So the problem is with your selector, not your code. – mTorres Jan 29 '17 at 08:19
  • Thank you. I'm trying to copy the inner text for this element:
    127.36
    . Does that mean I need to change the code to getElementsByCaseName()? Thanks
    – Edward Murray Jan 29 '17 at 09:07
  • Your problem is that the element is inside an iframe. You cannot directly refer to it, first, you have to get the document of the iframe, an then you can proceed. Now, I'm not sure how to do this, but it shouldn't be that hard, you can start [here](http://stackoverflow.com/questions/1088544/javascript-get-element-from-within-an-iframe). – mTorres Jan 29 '17 at 12:23

1 Answers1

0

So the issue is that the last-price-value element is contained within an iFrame- iFrames are a little bit of a pain and require special syntax and a few extra steps to retrieve their data- Basically you want to find the iFrame, get its HTML using .ContentDocument, and then can use that HTML document just like you normally would... however, this case of extracting data from an iFrame is particularly painful because the main web page is on one domain where the iFrame is on another domain, preventing you from accessing the iFrame's HTML from the main page... SO, what you have to do instead is: 1- Load the main page 2- Find the frame 3- Get the frame's URL 4- Load the frame URL 5- Pull the last-price-value..

See code example below:

Public Sub sampleCode()
Dim IE As New InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim parentURL As String
Dim frames As IHTMLElementCollection
Dim frameURL As String
Dim frameHTML As HTMLDocument
Dim fCounter As Long
Dim targetElement As HTMLObjectElement

parentURL = "http://www.morningstar.com/stocks/XASX/COH/quote.html"
'1- Load the main page
With IE
   .Visible = False
   .Navigate parentURL
    While .Busy Or .readyState <> READYSTATE_COMPLETE: Wend
    Set HTMLDoc = IE.document
End With

'2- Find the frame
Set frames = HTMLDoc.getElementsByTagName("iframe")
'Loop through all the frames
For fCounter = 0 To frames.Length - 1
    'Test each frame's ID to find the correct one (numerical appendix to ID can change so need to test for pre-fix)
    If Left(frames(fCounter).ID, 10) = "QT_IFRAME_" Then
        '3- Get the frame's URL
        frameURL = frames(fCounter).src
        Exit For
    End If
Next

'4- Load the frame URL
With IE
   .Navigate frameURL
    While .Busy Or .readyState <> READYSTATE_COMPLETE: Wend
    Set frameHTML = IE.document
End With

'Find and pull the last price
Set targetElement = frameHTML.getElementById("last-price-value")
Sheets("Sheet 1").Range("A2").Value = CDbl(targetElement.innerText)

IE.Quit
Set IE = Nothing
End Sub

Also, Yahoo has pretty easy VBA interface to pull stock prices so unless you need Morningstar because you are pulling prices on obscure mutual funds or private funds I would say you could make it easier on yourself by switching to Yahoo sources..

Hope this helps, TheSilkCode

TheSilkCode
  • 336
  • 2
  • 11