Download CSV File from GitHub

YasserKhalil :

I am trying to load csv file and download it to my hard disk from a link of GitHub. Here's my try

#If Win64 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As LongLong, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As LongLong, ByVal lpfnCB As LongLong) As LongLong
#Else
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
#End If

Function DownloadFile(Url As String, SavePathName As String) As Boolean
DownloadFile = URLDownloadToFile(0, Replace(Url, "\", "/"), SavePathName, 0, 0) = 0
End Function

Sub Demo()
Dim strUrl As String, strSavePath As String, strFile As String

strUrl = "https://github.com/pcm-dpc/COVID-19/blob/master/dati-regioni/dpc-covid19-ita-regioni-20200224.csv"    'SharePoint Path For The File
strSavePath = ThisWorkbook.Path & "\"
strFile = "FileName" & Format(Date, "dd.mm.yyyy") & ".csv"

If DownloadFile(strUrl, strSavePath & strFile) Then
    MsgBox "File Saved To: " & vbNewLine & strSavePath
Else
    MsgBox "Unable To Download File:" & vbNewLine & strFile & vbNewLine & "Check URL String And That Document Is Shared", vbCritical
End If
End Sub

I used the code before to download some files and it worked well but as for this link the downloaded file is as HTML page not csv file. How can I download it as CSV file?

Ryan Wildry :

Try hitting the raw content option on that page. The link you are hitting has HTML markup, the other is just the CSV content.

Option Explicit

Public Sub GetCSV()
    Dim response As String

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni-20200224.csv", False
        .Send
        response = .responseText
    End With

    If Trim$(response) = "" Then Exit Sub

    Open "YOURPATHHERE\SOMEFILE.csv" For Output As #1
    Print #1, response
    Close #1

End Sub

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=398115&siteId=1