Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Download file from website 2

Status
Not open for further replies.

twsjd

Technical User
Sep 18, 2002
5
US
I am trying to download a file from a website. I got this code off of tek-tips and it works great. The only problem i have is that if the file doesnt exist it shouldnt show a "File Not Found" messagbox.
I have not be able to figure out how to trap this error


oharab (Programmer) Oct 3, 2003
Typical, 2 minutes after you say you can't find it, you find it:

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

Private Const ERROR_SUCCESS As Long = 0

Public Function DownloadFile(sSourceUrl As String, _
sLocalFile As String) As Boolean

'if the API returns ERROR_SUCCESS (0),
'return True from the function
DownloadFile = URLDownloadToFile(0&, _
sSourceUrl, _
sLocalFile, _
0&, _
0&) = ERROR_SUCCESS

End Function

call it like:
DownloadFile "
and your file will be saved onto the c:\ drive.

hth

Ben
----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------


Thanks for the help
Jon Dabrowski
 
Thanks for that bit of code...I have been looking for something like this for a while now.

[yinyang]
 
Maybe I can answer your initial question...I called the DownloadFile() function in the following manner:

Code:
Dim FileAvailable As String

[green]'function DownloadFile will return either TRUE or FALSE
'which is assigned to the variable FileAvailable[/green]
FileAvailable = DownloadFile ("[URL unfurl="true"]http://Dataserver/excel/aa.zip"[/URL] , "C:\") 

If FileAvailable = FALSE Then
     MsgBox "File not found!"
Else
     MsgBox "File successfully downloaded"
End If

I guess you could have made FileAvailble a boolean type, but I haven't tried.



[yinyang]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top