Search code examples
excelvbastockquotesmsxml6

Parsing Fidelity Stock Quote page


I have been able to obtain stock and index data from Fidelity by setting up a connection to the relevant Fidelity page; writing the results to a worksheet; and then finding the results I want on that sheet.

I am now trying to simplify and avoid using both IE and the temporary worksheet page. The code below, using Microsoft XML, v6.0 seems to download all the relevant information, but I cannot figure out how to process things afterward so as to be able to extract the Name, Last Price, and Date/Time of that last price.

I have looked at Yahoo finance and Google finance, but they don't have all of the indices that I am interested in.

The code below gets me to the point of downloading the page information, but the only components that I see being returned by the "Get" is responseText (which I have split up and placed on a worksheet for examination), and responseBody which seems to be an array of character codes of the contents of responseText.

Perhaps I shouldn't even be using the MS XML tool in the first place, but it seems to me this should be possible.

Thanks for any pointers.


Sub FIDOQuoteTester()

'Set Reference to Microsoft XML V6.0

Const sURLPart1 As String = "http://quotes.fidelity.com/webxpress/get_quote?QUOTE_TYPE=D&SID_VALUE_ID="
Const sURLPart3 As String = "&submit=Quote"
Dim sURL As String
Dim sSymbols  As String '+ separated list
Dim REQ As MSXML2.ServerXMLHTTP60 'using Server type as the other raises security problems

Dim V As Variant, R As Range

Dim S As String

sSymbols = ".DJI+.SPX"
sURL = sURLPart1 & sSymbols & sURLPart3

Set REQ = New ServerXMLHTTP60
REQ.Open "Get", sURL, False
REQ.send

S = REQ.responseText

Stop

'putting string on worksheet for examination
'Need to split the string as it is too long for a cell.
V = Split(S, vbLf)
Set R = Range("a1").Resize(UBound(V) + 1, 1)
With R
    .EntireColumn.Clear
    .Value = WorksheetFunction.Transpose(V)
End With


End Sub


Solution

  • Set a refernce to Microsoft HTML Object Library.

    Then :

    '/ Adding response text to  MSHTML
    
    Dim oHTML       As New HTMLDocument
    Dim e           '/ This is your element
    
    oHTML.body.innerHTML = ""
    
    oHTML.body.innerHTML = S '/ Set your responseText(HTML) here
    
    '/ Loop through all the elements
    For Each e In oHTML.all
        Debug.Print e.ID & "-->" & e.innerText
    Next