Consulting

Results 1 to 5 of 5

Thread: Download from Excel via URLDownloadToFile

  1. #1
    VBAX Newbie
    Joined
    Jul 2010
    Posts
    1
    Location

    Download from Excel via URLDownloadToFile

    Hello everybody

    I'm wondering if you could help me. I'm trying to download a file on the web from Excel via the URLDownloadToFile functions.

    When i run the code below, it seems to work and i get the Msg box saying the download is finished but when i look at the folder, the file isn't there.

    Do you know what is happening ?

    Thank you for you help
    Gilles


    [VBA]'Declarations
    Private 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

    'Download Code
    Sub download()

    URLDownloadToFile 0, "mylink", "C:/cdm.xls", 0, 0
    MsgBox "File have been downloaded!"

    End Sub[/VBA]

  2. #2
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi

    1) You are not checking for success or failure so you will always get the happy little msg.


    2) "mylink" is a string. In other words it is the word mylink. If is meant to be variable contining a valid Web site it should not have quotes. (It also needs to be Dimmed and given a value..)

    [VBA]
    Option Explicit
    'Declarations
    Private 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

    'Download Code
    Sub download()

    Dim done

    'This will provide a return value to test.
    'Note the ( ) around the args
    done = URLDownloadToFile(0, "mylink", "C:/cdm.xls", 0, 0)

    'Test.
    If done = 0 Then
    MsgBox "File has been downloaded!"
    Else
    MsgBox "File not found!"
    End If

    End Sub

    [/VBA]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  3. #3
    VBAX Contributor
    Joined
    May 2010
    Location
    Sydney, NSW, Australia
    Posts
    170
    Location
    Does the slash in the location need to be the other way?

  4. #4
    VBAX Newbie
    Joined
    Mar 2013
    Posts
    1
    Location

    Can't save to root in Windows 7

    Try something like this:

    mylink = "websiteaddressdotcomslashfilename"

    myResult = URLDownloadToFile (0, mylink, "C:\Users\" & Environ("username") & "\" & "cdm.xls", 0, 0)

    If myResult <> 0 then
    Msgbox "Error downloading " & mylink & Chr(10) & Error(myResult)
    '{Additional error handling code goes here}
    Else
    Msgbox "File cdm.xls has been downloaded"
    End If
    Last edited by donato; 03-16-2013 at 01:42 PM.

  5. #5
    VBAX Newbie
    Joined
    Mar 2015
    Posts
    1
    Location
    Hi I tried this but all the files are about 9kb. Thanks!

Posting Permissions

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