Forum Discussion
save an image from a URL in excel
- Apr 25, 2020
drnick66 hello there!
I wouldn't qualify the question as "basic". It's a little advanced with VBA I'd say. Below is an example of how you can download a list of files via their URL. It will save the name with the same name it has on the web. Note there are no checks to see if the file exists and the destination folder is hard-coded (change to desired).
Option Explicit #If VBA7 Then 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 LongPtr #Else 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 #End If Public Sub DownloadImages() Dim Downloaded As Boolean Dim Count As Long Dim Index As Long Dim FileName As String Dim NameParts As Variant Dim Values As Variant Const DownloadPath As String = "C:\Users\Zack\Desktop\" Values = ThisWorkbook.Worksheets("Sheet1").Range("A2:A6").Value For Index = LBound(Values, 1) To UBound(Values, 1) NameParts = Split(Values(Index, 1), "/") FileName = NameParts(UBound(NameParts)) Downloaded = URLDownloadToFile(0, Values(Index, 1), DownloadPath & FileName, 0, 0) = 0 If Downloaded Then Count = Count + 1 Next Index MsgBox Count & " files out of " & UBound(Values, 1) - LBound(Values, 1) + 1 & " downloaded." End SubHTH
Hi Zack Barresse
Over here asking for help. I have a file I need to work on. Ive been searching about this topic and this answer pop up, which I find super helpful. Been trying to use the code on macro but it shows there is an error. Im pretty much new with macros but kinda get it.
I need to download the images that are on an excel file The URLs are in column B, I also need to assign them the name in column A. I'm trying to use this macro but it doesn't work. I have a mac with ios 12.6 software and using excel version 16,65 in Spanish (don't know if that would change the macro).
Thank you Zack so much for this code. Worked first time through.
Richard