473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,322 developers and data experts.

How to Download A File From Internet using VBA

Rabbit
12,516 Expert Mod 8TB
I often use this to download a CSV to update data in a database.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. 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
  4. 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
  5. Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
  6.  
  7. Declare Function SystemParametersInfo Lib "user32" _
  8.       Alias "SystemParametersInfoA" (ByVal iAction As Long, _
  9.       ByVal iParam As Long, pvParam As Any, _
  10.       ByVal fWinIni As Long) As Long
  11.  
  12. 'Purpose     :  Retreview text from a web site
  13. 'Inputs      :  sURLFileName            The URL and file name to download.
  14. '               sSaveToFile             The filename to save the file to.
  15. '               [bOverwriteExisting]    If True overwrites the file if it exists
  16. 'Outputs     :  Returns True on success.
  17. Function InternetGetFile(sURLFileName As String, sSaveToFile As String, Optional bOverwriteExisting As Boolean = False) As Boolean
  18.     Dim lRet As Long
  19.     Const S_OK As Long = 0, E_OUTOFMEMORY = &H8007000E
  20.     Const INTERNET_OPEN_TYPE_PRECONFIG = 0, INTERNET_FLAG_EXISTING_CONNECT = &H20000000
  21.     Const INTERNET_OPEN_TYPE_DIRECT = 1, INTERNET_OPEN_TYPE_PROXY = 3
  22.     Const INTERNET_FLAG_RELOAD = &H80000000
  23.  
  24.     On Error Resume Next
  25.     DoCmd.Hourglass True
  26.  
  27.     'Create an internet connection
  28.     lRet = InternetOpen("", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
  29.  
  30.     If bOverwriteExisting Then
  31.         If Len(Dir$(sSaveToFile)) Then
  32.             VBA.Kill sSaveToFile
  33.         End If
  34.     End If
  35.  
  36.     'Check file doesn't already exist
  37.     If Len(Dir$(sSaveToFile)) = 0 Then
  38.         'Download file
  39.         lRet = URLDownloadToFile(0&, sURLFileName, sSaveToFile, 0&, 0)
  40.         If Len(Dir$(sSaveToFile)) Then
  41.             'File successfully downloaded
  42.             InternetGetFile = True
  43.         Else
  44.             'Failed to download file
  45.             If lRet = E_OUTOFMEMORY Then
  46.                 Debug.Print "The buffer length is invalid or there was insufficient memory to complete the operation."
  47.             Else
  48.                 Debug.Assert False
  49.                 Debug.Print "Error occurred " & lRet & " (this is probably a proxy server error)."
  50.             End If
  51.             InternetGetFile = False
  52.         End If
  53.     End If
  54.  
  55.     On Error GoTo 0
  56.     DoCmd.Hourglass False
  57. End Function
  58.  
Jan 5 '11 #1
0 15965

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Rea Peleg | last post by:
link to configuration file prevents user control download to internet explorer Hi I have a simple web application which consists an opening html page with a (windows form user) control which...
0
by: Ilia Makarenko | last post by:
Hi everyone! I've got a problem with IIS 6.0 when downloading files with size > 4 Mb. So I am trying to download file in chunks, but it doesn't work. Only pert of the file is downloaded (~6.5 of...
4
by: mphanke | last post by:
Hi, how can I connect to a restricted directory on a server and download a file, if there is a newer version, than the one on the local file system? Something like ...
1
by: user2008 | last post by:
Hi all, I want to track how many times visitor download file from my website, for example, when visitor click on a download link, a ASPX page will be requested, after that it will redirect to a...
0
by: bonita | last post by:
If I add the code for user to download the file (e.g. if(File.Exists(FILE_NAME)){......}), the ASP.NET will give the following timeout error: Timeout expired. The timeout period elapsed prior to...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
8
by: BiT | last post by:
Hello, I'm working right now on project in vb.net 2005 for my company, i need the project to download file from the company web site. In order to get the file i have to give the site address...
3
by: cypherkro | last post by:
Hi I have a requirement to automatically download a file using a WebBrowser control without having the "Download File" pop up windows display. I cannot use Webclient!! I am using VS2005, .net2...
5
by: vinodkus | last post by:
dear sir/madam I have to write a code for download file using asp. please help me thanks in advance
1
by: vikassawant | last post by:
Hi, I m facing some critical problem for downloading 5mb file from server. Does anyone know? How to reduce the time to download file from server using HTTP connection?
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.