none
Get HTML Code into String from Newly clicked webpage (i.e. URL doesn't change) RRS feed

  • Question

  • Hi guys,

    So I'm trying to get the HTML source code from a website, except the website URL never changes. This is what I have so far/what I need help with:

    Sub MyVBA()

      Dim ie, objDoc, htmlInput As Object

      Dim strURI, htmlDOC As String

      struURI = "www.somewebsite.com"

      Set ie = CreateObject("internetexplorer.application")

      ie.Navigate strURI

      Do
          If ie.ReadyState = 4 Then
          ie.Visible = True
          Exit Do
          Else
       
          DoEvents
          End If
          Loop

      Set objDoc = ie.document

      Set htmlInput = objDoc.getElementById("insertIDhere")
      htmlInput.Click

      htmlDOC = objDOC.body.innerHTML

      msgBox(htmlDOC)

    End Sub

    'So VBA clicks on the button that I want, and InternetExplorer goes to the new webpage (this part of my code works correctly). The URL, however, never changes. So when I try to get the HTML source code of the new webpage through VBA, VBA gets the HTML source from the original webpage, not the new webpage that I want.

    Is there a way to get the source code from the new webpage, not the original webpage?

    Thanks!

    Thursday, March 7, 2013 12:48 AM

Answers

  • Quick search got me:

    http://vba-corner.livejournal.com/4623.html

    Very good article on manipulating html page with VBA. I added the following to get the html:

        myIE.Refresh
        Do While myIE.ReadyState <> READYSTATE_COMPLETE
          DoEvents
        Loop
        Debug.Print myIE.Document.body.innerHTML
        myIE.Quit
        Set myIE = Nothing


    Rod Gill

    The one and only Project VBA Book

    Rod Gill Project Management

    • Marked as answer by BcBc123 Monday, March 11, 2013 11:02 PM
    Thursday, March 7, 2013 8:59 PM

All replies

  • Your code is probably reading from a cached copy of the page, so try

    ie.Refresh after the loop


    Rod Gill

    The one and only Project VBA Book

    Rod Gill Project Management

    Thursday, March 7, 2013 3:43 AM
  • hmmm, when I do that I get an error that says:

    "Run-time error '-2147467259 (80004005)':

    Automation error

    Unspecified Error"

    When I press debug it highlights Set ie = CreateObject("internetexplorer.application")

    Thursday, March 7, 2013 5:58 PM
  • Here is exactly what I'm trying to do, if this helps:

    Sub MyVBA()

    Dim ie, objDoc, htmlInput As Object
    Dim strURI, htmlDOC As String

        strURI = "www[dot]cefconnect[DOT]com/Details/Summary[DOT]aspx?ticker=ISL"

    Set ie = CreateObject("internetexplorer.application")
        ie.Navigate strURI

    Do
    If ie.ReadyState = 4 Then
                ie.Visible = True
    Exit Do

    Else
                DoEvents
    End If
    Loop

    Set objDoc = ie.document

        'click on the pricing history tab
    Set htmlInput = objDoc.getElementById("__tab_ctl00_contents_SummaryContainer_PricingTab")
        htmlInput.Click

        'Change the date to 1/25/13
        Set htmlInput = objDoc.getElementById("ctl00_contents_SummaryContainer_PricingTab_ucPricing  _PriceDatePicker_dateInput")
        htmlInput.Value = "2013-01-25-00-00-00"

        'Click GO
        Set htmlInput = objDoc.getElementById("ctl00_contents_SummaryContainer_PricingTab_ucPricing  _PriceHistoryGo")
        htmlInput.Click

        'get the new source code (notice the URL never changes, but the source code does change)
        htmlDOC = objDOC.body.innerHTML
       

    'the source code in the msgBox is from the previous webpage, not the new one that I want

        msgBox(htmlDOC)

    End Sub

    Thursday, March 7, 2013 6:46 PM
  • Quick search got me:

    http://vba-corner.livejournal.com/4623.html

    Very good article on manipulating html page with VBA. I added the following to get the html:

        myIE.Refresh
        Do While myIE.ReadyState <> READYSTATE_COMPLETE
          DoEvents
        Loop
        Debug.Print myIE.Document.body.innerHTML
        myIE.Quit
        Set myIE = Nothing


    Rod Gill

    The one and only Project VBA Book

    Rod Gill Project Management

    • Marked as answer by BcBc123 Monday, March 11, 2013 11:02 PM
    Thursday, March 7, 2013 8:59 PM