I received a code from a website that requests login information to download a CSV file. Thanks to this website, I was able to get a code that I could modify to suit my needs. The code that applies to me is:
Option Explicit
Private Declare Function URLDownloadToFileA Lib "urlmon" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
Private Function DownloadUrlFile(URL As String, LocalFilename As String) As Boolean
Dim RetVal As Long
RetVal = URLDownloadToFileA(0, URL, LocalFilename, 0, 0)
If RetVal = 0 Then DownloadUrlFile = True
End Function
Sub DESCARGAR_CSV_DATOS()
Dim EstaURL As String
Dim EsteCSV As String
EstaURL = "https://user:token@www.privatewebsite.com/export/targetfile.csv"
EsteCSV = "MyCSV " & Format(Date, "dd-mm-yyyy") & ".csv"
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
DownloadUrlFile EstaURL, _
ThisWorkbook.Path & "\" & EsteCSV
DoEvents
Workbooks.Open ThisWorkbook.Path & "\" & EsteCSV, , True, , , , , , , , , , , True
'rest is just doing operations and calculations inside workbook
End Sub
I'm sorry, but I'm unable to give the actual URL. Anyway, since September 2019, this code has been flawless. It continues to function well every day.
All of the computers running this code are 64-bit Windows 7 machines with Excel 2007. All of them succeed.
However, this work will now be delegated to a different office. The machines there run Windows 10 64-bit and Excel 2019.
Additionally, the code is useless there. On Excel 2019 + W10, there is no problem, yet the function DownloadUrlFile does not download any files.
Hope somebody can throw some light on this.