Create a download macro that picks up URL from cell value

Fishboy

Well-known Member
Joined
Feb 13, 2015
Messages
4,267
Hi all,


I have incorporated a macro which on button press goes to a pre-defined web address (which is a direct link to a download), then saves to a pre-defined as a pre-defined filename. The code I am currently using is as follows:


Sub Download_CSV()
Application.ScreenUpdating = False
Dim myURL As String
myURL = "www.somewebsite.com/mydownload"

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send

myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.ResponseBody
oStream.SaveToFile ("C:\My Folder\Filename.csv"), 2
oStream.Close
End If
End Sub


What I would like to do is to incorporate the ability for the URL and destination filepath to be defined by values on the main spreadsheet.


For example, on my spreadsheet I have a cell containing my desired URL (lets say A1 for arguements sake) and that would allow the macro to interpret

myURL = Worksheets("Sheet1").Range("A1").Value or something similar.


I would also like to do the same thing for the save filepath, so lets say A2 has a value of C:\My Folder\Filename.csv, the macro would be able to interpret

oStream.SaveToFile (Worksheets("Sheet1").Range("A2").Value), 2


Is this something that can be worked into my code? Do I need entirely new code to accommodate this? Can this not be done at all?


Any help is, as always, greatly appreciated.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hmmm, bumping.

I don't know if nowadays I am just asking for more than Excel is capable of, or I am asking things that none of the experts understand, but this current query and my last three topics have not so much as had a "Sorry, dont know how to help" response from anybody.
 
Upvote 0
What I would like to do is to incorporate the ability for the URL and destination filepath to be defined by values on the main spreadsheet.


For example, on my spreadsheet I have a cell containing my desired URL (lets say A1 for arguements sake) and that would allow the macro to interpret

myURL = Worksheets("Sheet1").Range("A1").Value or something similar.


I would also like to do the same thing for the save filepath, so lets say A2 has a value of C:\My Folder\Filename.csv, the macro would be able to interpret

oStream.SaveToFile (Worksheets("Sheet1").Range("A2").Value), 2
You've answered your own question. That is exactly the code you would use. Replace the first myURL = statement with the line above (you don't need the second myURL = statement), and the SaveToFile statement with the line above.
 
Upvote 0
Thanks John_W, I must have made a typo or something when I originally tried this as it didn't work, but using my own examples from here fixed the problem!

Thank you for taking the time to look this over for me.
 
Upvote 0

Forum statistics

Threads
1,216,558
Messages
6,131,400
Members
449,648
Latest member
kyouryo

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