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, 2022 in Others by Kithuzzz
• 38,000 points
1,763 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, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

How can I scrape a excel file from a website and divide it in different parts?

Use Scrapy or beautifulsoup4 parsing data it's more convenient ...READ MORE

answered Jan 13, 2023 in Others by narikkadan
• 63,600 points
659 views
0 votes
1 answer

Is it possible to get data from a webpage in real-time to an excel file?

The conventional method of obtaining data from ...READ MORE

answered Jan 17, 2023 in Others by narikkadan
• 63,600 points
525 views
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, 2022 in Others by narikkadan
• 63,600 points
2,272 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, 2022 in Others by narikkadan
• 63,600 points
820 views
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,740 points
1,212 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
3,662 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, 2022 in Others by gaurav
• 23,260 points
908 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, 2022 in Others by Edureka
• 13,690 points
1,053 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, 2022 in Others by narikkadan
• 63,600 points
3,634 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