Consulting

Results 1 to 8 of 8

Thread: Solved: Downloading Excel File from Website

  1. #1

    Solved: Downloading Excel File from Website

    Hi,

    Previously on this site I was kindly helped by a couple of people to download a txt file from a website from within excel. The code is below and works fine.

    [VBA]Public Const FilePathX As String = "C:\"
    Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
    (ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) As Long[/VBA]

    URLDownloadToFile 0, "http://www.federalreserve.gov/releas...st/h6hist9.txt", FilePathX & "exampledata.txt", 0, 0

    Does anyone know what the syntax should be to download a data file in excel format from the OECD website?

    For example pasting the below line into web broswer takes you to data table that I want to download as an excel file from within excel.

    http://stats.oecd.org/wbos/Index.asp...MEI_CLI&lang=e

    There is way export the data manually as a excel file (or csv) online by clicking on the export excel icon. But I'm struggling to work out what it should be if I want to go straight to the excel file.

    Thanks,

    Nick

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Maybe just grab the table: see attached
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Since they are using JavaScript, SendKeys() would be the only way to Export Excel. If you have Vista, you will have to disable UAC if you want to pursue that route.

  4. #4
    Gents,

    Thanks for your suggestions. Lucas's solution will work for me. Just one further queston. In order to update the table can I just refresh the query or would I have to run the macro from scratch?

    Thanks,

    Nick

  5. #5
    Hello All,

    I tried downloading the table using the webquery macro below but it no longer seems to work. But when you paste the link onto the website it takes you to the right page and table? Any ideas why it's not working anymore?

    Thanks,

    Nick

    http://stats.oecd.org/wbos/Index.asp...MEI_CLI&lang=e", _

    Sub gethtmltable()
    Dim objWeb As QueryTable
    Dim sWebTable As String
    'You have to count down the tables on the URL listed in your query
    'This example shows how to retrieve the 2nd table from the web page.
    sWebTable = 10
    'Sets the url to run the query and the destination in the excel file
    'You can change both to suit your needs
    Set objWeb = ActiveSheet.QueryTables.Add( _
    Connection:="URL;http://stats.oecd.org/wbos/Index.aspx?datasetcode=MEI_CLI&lang=e", _
    Destination:=Range("A1"))

    With objWeb

    .WebSelectionType = xlSpecifiedTables
    .WebTables = sWebTable
    .Refresh BackgroundQuery:=False
    .SaveData = True
    End With
    Set objWeb = Nothing
    End Sub

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You have to get the right table:

    [VBA]sWebTable = 5[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    Hello Lucas,

    Changing the line: sWebTable = 10 to sWebTable = 5 now works.

    But I'm slightly confused - a couple of questions.

    First, why has the table number changed from 10 to 5? The query with the 10 worked before, I would assumed the table number would stay the same.

    Second how did you work out the table number was 5 or 10 the first time? Is it easy to work out?

    Thanks,

    Nick

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Nick, I can only assume that the website made changes that changed the table number and it was just trial and error this time as I was in a hurry. I just started with a 1 and worked up until I found your table.

    You can look at the source code and figure it out by finding each instance of <Table>

    Just right click on the page and select "view source"
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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