Errore URLDowloadTofile -2146697208

Ermitage

New Member
Joined
Jan 14, 2024
Messages
3
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi everyone,
I've been struggling with this problem for a few days now, so I would like to thank anyone who can help me I have a simple SUB which until recently worked perfectly and downloaded an excel file from the C.M.E. websitegroup
For a few days now on a PC on which M. Excel 365 is installed I have an error in the result (result in the VBA code) of the URLDownloadToFile command equal to -2146697208

I would like to point out that on a PC where I have installed M. Excel 2019 works perfectly,

I kindly ask for help especially in interpreting the error code

VBA Code:
Private Declare PtrSafe 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



Private Sub ScaricaFile()

    Url = "https://www.cmegroup.com/CmeWS/exp/voiProductDetailsViewExport.ctl?media=xls&tradeDate=20220225&reportType=P&productId=133"'

    dlpath = "C:\Users\OneDrive\Documents\My document\Option\"'

    result = URLDownloadToFile(0, Url, dlpath & "VoiDetailsForProduct.xls", 0, 0)

    If result <> 0 Then

        Application.Wait (Now + TimeValue("00:00:03")) ' Attendi 3 secondi e riprova

        result = URLDownloadToFile(0, Url, dlpath & "VoiDetailsForProduct.xls", 0, 0)

    End If

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi *Ermitage. Make sure that your one drive is syncing before you run the api. HTH. Dave
ps. Welcome to the Board!
 
Upvote 0
Also, pCaller and lpfnCB are pointers, and should be declared as LongPtr in an API function delcared as PtrSafe...

VBA Code:
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As LongPtr, _
                                                                                                ByVal szURL As String, _
                                                                                                ByVal szFileName As String, _
                                                                                                ByVal dwReserved As Long, _
                                                                                                ByVal lpfnCB As LongPtr) As Long

Hope this helps!
 
Upvote 0
Hi all,
thanks for you replies, unfortunately the error is still present, please can you try that code snd tell me your excel version?
 
Upvote 0
Hi again *Ermitage. Your local path has an extra apostrophe at the end of it ? Anyways, maybe the URL has changed? Ensure that the URL path is correct. We don't have your local drive to test the API and your local path doesn't look right to me. You can trial this...
Code:
 dlpath = (Environ("onedrive") & "\Documents\My document\Option\")
HTH. Dave
 
Upvote 0
Enter the following line of code in the Immediate Window (Visual Basic Editor >> View >> Immediate Window), and press ENTER...

VBA Code:
? Dir(Environ("onedrive") & "\Documents\My document\Option", vbDirectory)

If the path to your folder Option is correct, it should return the text "Option". Does it return "Option" ?
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top