VB and VBA Users Source Code: Download a file from the internet (without a prompt dialog)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Download a file from the internet (without a prompt dialog)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, August 24, 2001
Hits:
1260
Category:
Internet/Remote Comms
Article:
The following routine downloads a file from the internet without prompting the user first. Option Explicit 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 Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" (ByVal lpszUrlName As String) As Long 'Purpose : Retreview text from a web site 'Inputs : sURLFileName The URL and file name to download. ' sSaveToFile The filename to save the file to. ' [bOverwriteExisting] If True overwrites the file if it existings 'Outputs : Returns True on success. 'Author : Andrew Baker 'Date : 21/10/2000 12:37 'Notes : 'Revisions : Function InternetGetFile(sURLFileName As String, sSaveToFile As String, Optional bOverwriteExisting As Boolean = False, Optional bDeleteCache As Boolean = False) As Boolean Dim lRet As Long Const S_OK As Long = 0, E_OUTOFMEMORY = &H8007000E Const INTERNET_OPEN_TYPE_PRECONFIG = 0, INTERNET_FLAG_EXISTING_CONNECT = &H20000000 Const INTERNET_OPEN_TYPE_DIRECT = 1, INTERNET_OPEN_TYPE_PROXY = 3 Const INTERNET_FLAG_RELOAD = &H80000000 On Error Resume Next 'Create an internet connection lRet = InternetOpen("", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0) If bOverwriteExisting Then If Len(Dir$(sSaveToFile)) Then VBA.Kill sSaveToFile End If End If If bDeleteCache Then Call DeleteUrlCacheEntry(sURLFileName) End If 'Check file doesn't already exist If Len(Dir$(sSaveToFile)) = 0 Then 'Download file lRet = URLDownloadToFile(0&, sURLFileName, sSaveToFile, 0&, 0) If Len(Dir$(sSaveToFile)) Then 'File successfully downloaded InternetGetFile = True Else 'Failed to download file If lRet = E_OUTOFMEMORY Then Debug.Print "The buffer length is invalid or there was insufficient memory to complete the operation." Else Debug.Assert False Debug.Print "Error occurred " & lRet & " (this is probably a proxy server error)." End If InternetGetFile = False End If End If On Error GoTo 0 End Function 'Demonstration routine, downloads an image from vbusers.com 'to the c: drive Sub Test() If InternetGetFile("http://www.vbusers.com/images/default/top/top_navi_01.gif", "c:\top_navi_01.gif", True) Then MsgBox "Successfully downloaded file!" Else MsgBox "Failed to download file!" End If End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder