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
There may be caching. Try the following where a requestHeader is added to try and mitigate for potential caching. The other common alternative, adding a random number to the end of the URL doesn't seem to work for this site.
Option Explicit
Public Sub test()
Dim i As Long
For i = 1 To 3
DownloadFile "https://www.hko.gov.hk/tide/marine/data/ALL.txt"
Debug.Print Now
Application.Wait Now + TimeSerial(0, 5, 0)
Next
End Sub
Public Sub DownloadFile(ByVal link As String)
Dim WinHttpReq As Object, oStream As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
With WinHttpReq
.Open "GET", link, False
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
Debug.Print StrConv(.responsebody, vbUnicode)
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write .responsebody
oStream.SaveToFile ThisWorkbook.Path & "\raw\" & "temp.csv", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
End With
End Sub