URLDownloadToFile returns wonky value and doesn't download file

mathchick

Active Member
Joined
Mar 21, 2012
Messages
337
My first time posting, hopefully someone who is better at VBA than me is willing to help me out...

I know there are a lot of forum topics out there about URLDOwnloadToFile, but I haven't found anything addressing the issue I'm having. The code is running quite happily without any errors, but it isn't downloading the file. Without any errors I'm having a difficult time diagnosing the issue. I'm getting a very odd return value, however. Here's my code...

Code:
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
 Function downloadWeatherData(location As String)
    Dim done
    Dim myLink As String
    Dim locationNum As String
    
    Select Case location
    Case "AK - BARROW W POST-W ROGERS ARPT [NSA - ARM]"
        locationNum = "723230"
    End Select
    myLink = "http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/data/tmy3/" & locationNum & "TY.csv"
    
    done = URLDownloadToFile(0, "myLink", "C:\Users\***myname****\Documents\DownloadTest", 0, 0)
     
     'Test.
    If done = 0 Then
        MsgBox "File has been downloaded!"
    Else
        MsgBox "File not found!"
    End If
     
End Function

Purpose of this code is to retrieve a CSV file from a website depending on the location the user selects (thus the random select case block with only one option so far).

What I've figured out so far:
-I copied the value of myLink into a webbrowser to make sure that was correct, and it downloaded properly.
-verfied my file path by copying and pasting into windows explorer, it came up
-tried running it at home in case it was a firewall issue (can't change the firewall @ work), still didn't work with firewall off
-oddest: the value of done is -2146697203, which quite befuddles me
-set the return type as string rather than as long since the microsoft documentation seems to indicate a string will be returned, but this just caused excel to crash.

Possible issue source?:
I looked at Microsoft's documentation of this library function here, and wonder if the issue is with parameter pCaller, the rest seem fairly straightforward. This code is being called by a subroutine which is called from the click event of an activex control. I've been trying to wrap my head around the description of pCaller and just have no idea what to do with it besides set it to zero like all the examples I've found online.

Other info:
Excel 2007
Windows 7 professional @ work, Windows 7 home @ home


Any help would be much appreciated, I've been puzzling over this for the better part of a day with no progress and no idea what else to try.

~Mathchick
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Solved!

I found a different solution that works quite well. Adapted from here:
http://www.mrexcel.com/forum/showthread.php?t=592933

Code:
Function transfercsv(location As String)
    Dim sCSVLink As String
    Dim sfile As String
    Dim locationNum As String
    Dim wnd
    
    Select Case location
    Case "AK - BARROW W POST-W ROGERS ARPT [NSA - ARM]"
        locationNum = "723230"
    End Select
    sCSVLink = "http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/data/tmy3/" & locationNum & "TY.csv"
    
    sfile = locationNum & "TY.csv"
    
    Set wnd = ActiveWindow
    Application.ScreenUpdating = False
    Sheets("CSV Transfer").Cells.ClearContents
    Workbooks.Open Filename:=sCSVLink
    'Windows(sfile).Activate
    ActiveSheet.Cells.Copy
    wnd.Activate
    Workbooks("SpreadsheetDraft").Sheets("CSV Transfer").Range("A1").Select
    Workbooks("SpreadsheetDraft").Sheets("CSV Transfer").Paste
    Application.DisplayAlerts = False
    Windows(sfile).Close False
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Function


Alas, it takes about 21 seconds to run with decent internet speed, so it looks like I'm going to be parsing out all of the possible downloads and including them with the spreadsheet, but at least I learned how to do this in case I have to in the future, and this could possibly be useful to some other VBA-er.

thanks for any glances this got today,
~Christy
 
Upvote 0
Hi Mathchick

I have the same problem you were having with URLDOwnloadToFile, and the 'done' variable also has a value of -2146697203.

Unlike you, I need to download a zip file, therefore it cannot be solved by the solution you were able to use for the csv files.

There isn't much help on the fact that URLDOwnloadToFile doesn't do anything. I'm surprised that there aren't more people out there with this problem.

Any suggestions?
 
Upvote 0
for the sake of having all the info in one place, and because it took me a while to understand this all a little bit, here are some valuable info related to this problem:

Here: "Microsoft learn website" it states that URLDownloadToFile returns a HRESULT value
Here are some HRESULT values (the most common), the others can be gathered at this link

But the -2146697203 = &H800C000D was not there, not also the INET_E_DOWNLOAD_FAILURE. Searching for this last one, reached this link, where the -2146697203 matches : -
Constant= INET_E_UNKNOWN_PROTOCOL value=&H800C000D Description=The protocol is not known and no pluggable protocols have been entered that match.

Hope it can help someone in the future
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,099
Members
449,096
Latest member
provoking

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