PDA

View Full Version : Download from Excel via URLDownloadToFile



langevin
07-19-2010, 10:50 AM
Hello everybody :hi:

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


'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

rbrhodes
07-19-2010, 11:30 PM
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..)


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

Blade Hunter
07-19-2010, 11:44 PM
Does the slash in the location need to be the other way?

donato
03-16-2013, 01:26 PM
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

tlf179er
12-07-2015, 11:04 AM
Hi I tried this but all the files are about 9kb. Thanks!