Download from web

  • Activate the below mentioned options
  •  

     

  • Micorsoft Internet Controls is userful to open INTERNET
  •  

  • Microsoft HTML Object is Liabrary is useful to copy the TARGET(HTML format) web page
  •  

    Code for Google Search

     

     

    Open Required Webpage

     

    Private Sub CommandButton1_Click()
    Dim ie As InternetExplorer
    Set ie = New InternetExplorer
    ie.Visible = True
    ie.navigate "https://in.finance.yahoo.com"
    End Sub

     

  • Visible Property enbales to see the window
  • Navigate Property denotes about the web page, we intends to open
  •  

     

     

    Capture the Pricing Data - Single company

     

    Private Sub CommandButton1_Click()
    Dim ie As InternetExplorer
    Set ie = New InternetExplorer
    Dim ticker As String
    ticker = Sheets("Sheet2").Range("A2").Value
    ie.Visible = True
    ie.navigate "https://in.finance.yahoo.com/q?s=" & ticker
    Dim doc As HTMLDocument
    Do While ie.readyState <> READYSTATE_COMPLETE
    'Do Until ie.readyState = READYSTATE_COMPLETE
    Set doc = ie.document
    Loop
    On Error Resume Next
    Sheets("Sheet2").Range("B2").Value = doc.getElementById("yfs_l84_" & ticker).innerText
    MsgBox "hi pricing updated"
    ie.Quit
    End Sub

     

     

     

    Capture the Pricing Data - Multiple Companies

     

     

    Private Sub CommandButton1_Click()
    Dim max As Integer
    max = Range("A1").End(xlDown).Row
    Dim ticker As String
    For i = 2 To max
    ticker = Sheets("Sheet2").Range("A" & i).Value
    Dim url As String
    url = "http://finance.yahoo.com/d/quotes.csv?s=" & ticker & "&f=nj2f6pd1j1e"
    Dim ht As New WinHttpRequest
    ht.Open "get", url
    ht.Send
    result = Split(ht.ResponseText, ",")
    Dim parts As Integer
    Dim j As Integer
    j = 2
    For parts = 0 To UBound(result)
    Cells(i, j).Value = result(parts)
    Columns(j).AutoFit
    j = j + 1
    Next
    UsedRange.WrapText = False
    Next
    End Sub

     

     

     

     

     

  • In this example i selected XLSPECIFIEDTABLES
  •  

  • refreshstyle Property
  •  

     

     

    Download Tables from YAHOO FINANCE

     

    Private Sub CommandButton1_Click()
    Dim qt As QueryTable
    Dim url As String
    url = "https://in.finance.yahoo.com/q?s=AAPL"
    Set qt = QueryTables.Add( _
    Connection:="URL;" & url, _
    Destination:=Sheets("Sheet1").Range("A1"))
    With qt
    .Name = "hi"
    .WebSelectionType = xlSpecifiedTables
    .WebFormatting = xlWebFormattingNone
    .WebTables = "2,3"
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableRedirections = True
    .FieldNames = True
    .RefreshPeriod = 0
    .RefreshStyle = xlInsertDeleteCells
    .AdjustColumnWidth = True
    .Refresh BackgroundQuery:=False
    End Sub

     

     

    Loop through All Elements

     

  • It Iterates through all elements ie. Class, Tag, ID ...
  •  

  • Element count starts from Zero(o), hence we need to mention -1 at the end of elements count
  •  

    If Element inner text is "HI" then on message box element+2 Innertext will display

    For ElementNumber = 0 To Ie.Document.all.Length - 1
    If IE.Document.all.Item(ElementNumber).innerText = "Hi" Then
    MsgBox IE.Document.all.Item(ElementNumber + 2).Innertext
    End If
    Next

     

     

    GetElementsbyTagName - Frame

     

  • We can retrieve all the FRAMES data
  •  

    Set FrameNumbers = ie.document.getElementsByTagName("Frame")
    i = 0
    For Each FrameNumber In FrameNumbers
    Cells(i + 1, 1).Value = FrameNumber.getAttribute("src")
    Cells(i + 1, 2).Value = FrameNumber.getAttribute("id")
    Cells(i + 1, 3).Value = FrameNumber.Innertext
    i = i + 1
    Next

     

    GetElementsbyTagName - a

     

  • Click on Hyperlink based on Innertext "Hi"
  •  

    Set Hlinks = ie.document.getElementsByTagName("a")
    For Each link In Hlinks
    If link.innerText = "Hi" Then
    link.Click
    Exit For
    End If
    Next