+ Reply to Thread
Results 1 to 5 of 5

Downloading online files using VBA

  1. #1
    Registered User
    Join Date
    11-21-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    10

    Downloading online files using VBA

    Morning all,

    I'm trying to find out how to download an online xls file and save it locally using vba?

    I have the code working fine for a pdf document (see below), but cant get it to work when trying to download a .xls or .doc file

    If anyone could point me in the right direction - that would be great.

    Thanks
    Kurt


    Sub DownloadFiles()

    Dim FileNum As Long
    Dim FileData() As Byte
    Dim MyFile As String
    Dim PDFFile As String
    Dim WHTTP As Object



    ' Create connections to IE
    On Error Resume Next
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
    If Err.Number <> 0 Then
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
    End If
    On Error GoTo 0

    PDFFile = "WhitechurchWD.pdf"
    MyFile = "http://www.ifa-systems-uk.co.uk/webs/whitechurch1982/documents/WMQ42013v2Small.pdf"


    'Now we go and get the file and save it to the C:\Temp folder
    WHTTP.Open "GET", MyFile, False
    WHTTP.Send
    FileData = WHTTP.responseBody
    Set WHTTP = Nothing

    If Dir("C:\Temp", vbDirectory) = Empty Then MkDir "C:\Temp"

    FileNum = FreeFile
    Open "C:\Temp\" & MyFile For Binary Access Write As #FileNum
    Put #FileNum, 1, FileData
    Close #FileNum


    End Sub

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Thumbs up Re: Downloading online files using VBA

    Please Login or Register  to view this content.
    Last edited by patel45; 11-21-2013 at 07:44 AM.
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    11-21-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Downloading online files using VBA

    Thanks for you suggestion patel45.

    My actual error was a silly typo (apologies).

    All sorted now. If anyone else needs this code - here you go:


    Sub DownloadMyFiles()

    Dim FileNum As Long
    Dim FileData() As Byte
    Dim PDFFile As String
    Dim MyPDFFile As String
    Dim XLFile As String
    Dim MyXLFile As String
    Dim WHTTP As Object

    ' Create connections to IE
    On Error Resume Next
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
    If Err.Number <> 0 Then
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
    End If
    On Error GoTo 0

    ' ------------------------------------------------------------------------------------------------
    PDFFile = "SamplePDF.pdf"
    MyPDFFile = "http://www.ifa-systems-uk.co.uk/webs/whitechurch1982/documents/WMQ42013v2Small.pdf"

    ' ------------------------------------------------------------------------------------------------

    'Now we go and get the file and save it to the C:\Temp folder
    WHTTP.Open "GET", MyPDFFile, False
    WHTTP.Send
    FileData = WHTTP.responseBody
    Set WHTTP = Nothing

    If Dir("C:\Temp", vbDirectory) = Empty Then MkDir "C:\Temp"

    FileNum = FreeFile
    Open "C:\Temp\" & PDFFile For Binary Access Write As #FileNum
    Put #FileNum, 1, FileData
    Close #FileNum
    ' ----------------------------------------------------------------------------------

    'Now the Xl file

    ' Create connections to IE
    On Error Resume Next
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
    If Err.Number <> 0 Then
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
    End If
    On Error GoTo 0

    XLFile = "SampleExcel.xls"
    MyXLFile = "http://www.exinfm.com/excel files/fcfe2st.xls"


    WHTTP.Open "GET", MyXLFile, False
    WHTTP.Send
    FileData = WHTTP.responseBody
    Set WHTTP = Nothing

    If Dir("C:\Temp", vbDirectory) = Empty Then MkDir "C:\Temp"

    FileNum = FreeFile
    Open "C:\Temp\" & XLFile For Binary Access Write As #FileNum
    Put #FileNum, 1, FileData
    Close #FileNum

    MsgBox ("Online file downloads complete - remove this msg after testing")

    End Sub

  4. #4
    Forum Contributor
    Join Date
    02-24-2013
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    317

    Re: Downloading online files using VBA

    Kurt,

    Just so you know, the forum rule is to put code tags around your code so that it appears like patel45 did.

    before the code, type this; "['code]"
    after the code, type this; "['/code]"

    just don't type in the single ' after the square bracket. I had to do that so the website didn't turn that into a code field.

    Please Login or Register  to view this content.
    Last edited by Jim885; 11-21-2013 at 09:07 AM.

  5. #5
    Registered User
    Join Date
    11-21-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Downloading online files using VBA

    Ahh - Thanks Jim885

    I get you - Like this:


    "
    Please Login or Register  to view this content.
    "

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Downloading files
    By Alf in forum Suggestions for Improvement
    Replies: 3
    Last Post: 07-17-2013, 04:26 AM
  2. downloading files with macros
    By nygwnj in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-17-2007, 06:08 PM
  3. Downloading Finished Excel Labs Online?
    By crashxdjp in forum Excel General
    Replies: 0
    Last Post: 11-21-2005, 01:12 PM
  4. Downloading multiple files
    By tekman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-18-2005, 08:33 AM
  5. [SOLVED] Downloading Files Using Excel
    By ditane in forum Excel General
    Replies: 0
    Last Post: 03-30-2005, 04:06 PM

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