Download file from URL in Excel 2019 it works on Excel 2007

0 votes

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.

Oct 7 in Others by Kithuzzz
• 20,660 points
132 views

1 answer to this question.

0 votes

The Sub Code looks fine. Check the references in VBA's tools menu and make the following declaration of ptrsafe.

Private Declare PtrSafe Function URLDownloadToFileA Lib "urlmon" _

enter image description here

answered Oct 7 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
1 answer

Force download excel file not working on web server

Change the content type to Content-Type: application/vnd.ms-excel Or try ...READ MORE

answered Oct 11 in Others by narikkadan
• 37,660 points
91 views
0 votes
1 answer

Create Graph from data in an excel file

Your first step would be to become ...READ MORE

answered Oct 17 in Others by narikkadan
• 37,660 points
42 views
0 votes
1 answer

Is it possible to round to different decimal places in excel based on the range?

Where the range is multiple cells: Excel.Worksheet sheet ...READ MORE

answered Nov 17 in Others by narikkadan
• 37,660 points
30 views
0 votes
2 answers
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,720 points
454 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
2,228 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9 in Others by gaurav
• 22,040 points
71 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16 in Others by Edureka
• 13,640 points
210 views
0 votes
1 answer
0 votes
1 answer

Unable to download excel file from src/assets in development/qa/prod environment for angular application

Try this: this.HttpClient.get("./assets/sample.xlsx",{responseType: "blob"}).subscribe((res:any) => ...READ MORE

answered Nov 12 in Others by narikkadan
• 37,660 points
94 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP