URL Download to File

Kristensen11

New Member
Joined
Jul 17, 2018
Messages
7
Hi All,

I need some help regarding downloading a picture and then save it in a folder.

Right now i have the following:


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

Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then
If Dir(LocalFilename) <> vbNullString Then
DownloadFile = True
End If
End If
End Function






Sub Savepicture()


Dim sSourceUrl As String
Dim sLocalFile As String

sSourceUrl = "http://www.image.com/img/home/hero-2.jpg"
sLocalFile = "C:\Users\Jens.Hansen\Desktop\Test\Seaworld"

If DownloadFile(sSourceUrl, sLocalFile) Then
MsgBox "File downloaded successfully", vbInformation
Else
MsgBox "There was a problem downloading the file!", vbInformation
End If


End Sub


However, when i run it, it says that there was a problem downloading the file - what is wrong?

Thank you ! :)
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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