Public Const INTERNET_OPEN_TYPE_DIRECT = 1
Public Const INTERNET_OPEN_TYPE_PROXY = 3
Public Const INTERNET_FLAG_RELOAD = &H80000000
Public 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
Public Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As Long
Public Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal sbuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetOpenUrl Lib "wininet" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Function GetFile(ByVal sURL As String) As Integer
' Reads the data from a web page to a buffer and downloads it to a user's machine
'
'
Dim hOpen As Long
Dim hFile As Long
Dim sbuffer As String
Dim Ret As Long
'Dim lFilenumber As Long
Dim oFS As Object
Dim oTxtStream As Object
On Error GoTo GetFile_Error
GetFile = 0
' create a buffer for the file we're going to download
' you'll have to decide ahead of time how large the file that is to be downloaded is and adjust accordingly
sbuffer = Space(2000)
' create an internet connection
hOpen = InternetOpen("userAgent", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
' open the url
hFile = InternetOpenUrl(hOpen, sURL, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&)
If hFile <> 0 Then
' read the bytes of the file
InternetReadFile hFile, sbuffer, 2000, Ret
' clean up
InternetCloseHandle hFile
InternetCloseHandle hOpen
' create file system object
Set oFS = CreateObject("Scripting.FileSystemObject")
' create a textstream - parameter of true means it will be unicode
Set oTxtStream = oFS.CreateTextFile("c:\PathToFile\myFile.txt", True)
' now we have our textstream we can write to it and then close it
oTxtStream.Write Trim$(sbuffer)
oTxtStream.Close
Set oTxtStream = Nothing
Set oFS = Nothing
GetFile = 1
End If
On Error GoTo 0
Exit Function
GetFile_Error:
MsgBox "Error " & Err.Number ' put your own handling here
End Function