Downloading File with VBA

Dohko

Board Regular
Joined
Apr 13, 2007
Messages
88
Hi,

How can I write some code to download a file from a website programatically?

Say I want to download abc.xls that is located at www.google.com/abc.xls
and save it to c:\temp\abc.xls.

Without getting any prompts, just overwrite the file if it exists.

Thanks.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,003
Have a look at the Windows API functions URLDownloadToFile or URLDownloadToCacheFile.
 

Dohko

Board Regular
Joined
Apr 13, 2007
Messages
88
CAn i do this in the background without opening the file?
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,003
Both those functions just download the file, they don't open it. As for in the background, maybe try calling one of them in an Application.OnTime procedure, or search for asynchronous download.

Alternatively, if the file is a .xls, as your URL indicates, Excel itself can download it. Try File - Open - the URL with the macro recorder running to generate the VBA code.
 

Dohko

Board Regular
Joined
Apr 13, 2007
Messages
88
I tried downloading the file, but since its a CSV file it is not the original file once it is saved by excel.

This is what im doing..

Workbooks.Open Filename:="http://blabla.com/table.csv"
Application.DisplayAlerts = False
Windows("table.csv").Activate
ActiveWorkbook.SaveAs Filename:="C:\blabla\export.csv", FileFormat:=xlCSV _
, CreateBackup:=False
ActiveWorkbook.Close savechanges:=False

Can someone show me how to copy the file without opening it perhaps?
 

Forum statistics

Threads
1,141,734
Messages
5,708,157
Members
421,549
Latest member
Dtcfire

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
Top