+ Reply to Thread
Results 1 to 4 of 4

Vba to extract data from a web site

Hybrid View

  1. #1
    Registered User
    Join Date
    08-28-2012
    Location
    wisbech england
    MS-Off Ver
    Excel 2003
    Posts
    7

    Vba to extract data from a web site

    Hi all , this is my first post I hope I get across my idea clearly ( my first web VBA please be gentle ):

    What I want to achieve:

    1) open a web page http://www.bmreports.com/bwh_Mid.htm
    2) insert date and period in the text box and click "go"
    3) once the data is displayed I need to import them in eirther excel or access

    I found lot of problem in inserting data and periods in the textbox , unfortunately the web queries are not displayed in the browser
    Below the code I have used :
    Sub GetMarketPrice()
    Dim ie As Object, ipf As Object
    Dim strURL As String
    Dim tries
    
    Set ie = CreateObject("InternetExplorer.Application")
    strURL = "http://www.bmreports.com/bwh_Mid.htm"
    Dim ieObj As HTMLInputElement
    ie.Navigate strURL
    While ie.busy
      DoEvents  'wait until IE is done loading page.
    Wend
    
    
    'Set ieObj = ie.document.All("param0")
    'ieObj.Value = "2012-01-01" 'run-time error -91
    
    
    '###################################################################
    'enter date
    Set ipf = ie.document.getElementById("param0")
    ipf.Value = "2011-01-05" 'fill in the text box (here error)
    ie.document.All("param0").Value = "2011-01-01"
    '###################################################################
    
    '################################################################
    'I have also tried this: But againd failed
    ''Set ipf = ie.Document.getElementbytagname("param0")
    ''ipf.Value = "2011-01-05" 'fill in the text box
    '''Set ipf = IE.document.getElementByName("param0")
    ''ipf.Value = "2011-01-05" 'fill in the text box
    '###################################################################
    
    End Sub
    below the web code:
    HTML Code: 
    Last edited by Leith Ross; 08-28-2012 at 04:24 PM. Reason: Addec Code and HTML Tags

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Vba to extract data from a web site

    You don't need IE automation and HTML DOM parsing to extract this data. Instead, a web query is the simplest way, as long as you specify the correct parameters and values in the query string (the part of the URL after the ? character). Something like this:
    Sub Get_Data()
    
        Dim URL As String
        
        URL = "http://www.bmreports.com/servlet/com.logica.neta.bwp_MarketIndexServlet?param0=2012-06-20&param1=1&displayCsv=false"
        
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;" & URL, Destination:=Sheets("Sheet1").Range("A1"))
            .Name = "market_index"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlAllTables
            .WebFormatting = xlWebFormattingNone
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
        
    End Sub
    That's just a web query generated by the macro recorder and I tweaked the code to declare the URL string. In this example I hard-coded the date (param0) and period (param1) values, but you will want to specify your own values from user inputs. Use Format function to format the input date as yyyy-mm-dd: Format(theDate, "yyyy-mm-dd").
    Post responsibly. Search for excelforum.com

  3. #3
    Registered User
    Join Date
    08-28-2012
    Location
    wisbech england
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Vba to extract data from a web site

    thanks a lot it works beautifully, i also tried to build it but i missed displayCsv thank you very much

  4. #4
    Registered User
    Join Date
    08-28-2012
    Location
    wisbech england
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Vba to extract data from a web site

    in any case are u able to answer to my original question???( in case i would like to use IE automation and HTML DOM ) thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1