I wanted to download a file from this link: https://www.hko.gov.hk/tide/marine/data/ALL.txt
This file updates itself every 5 minutes. So I went on and create an excel VB
QHarr's answer did not work for me. I tried many solutions, but it seems either they did not work with VBA or there was a need for an early binding call, which I did not like so I came up with the following method. Simply, we will try to delete the same url/file from the cache and then proceed into downloading the file:
'--- copy this to the top of the module (below Option Explicit) ---
'Declaration for deleting a file from the cache:
Private Declare PtrSafe Function DeleteUrlCacheEntry Lib "wininet.dll" Alias _
"DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long
The function should be modified as follows:
Function DownloadFile(link As String)
Dim WinHttpReq As Object
'First delete the file from cache:
'On Error Resume Next
DeleteUrlCacheEntry url
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", link, False, "username", "password"
WinHttpReq.send
myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile ThisWorkbook.Path & "\raw\" & "temp.csv", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
End Function