VBA to download images and rename

sashaP

New Member
Joined
Jul 19, 2019
Messages
8
Hi

i have an excel file with links to images in column L i need to download them and rename them with the list in column M

Thanks !:):nya:
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try adapting the code at https://www.mrexcel.com/forum/excel...tead-excel-sheet-post5080413.html#post5080413

One change, to use the links in column L and save them as the names in column M:

Code:
    With ActiveSheet
        lr = .Cells(Rows.Count, "L").End(xlUp).row
        For r = 2 To lr
            DownloadFile .Cells(r, "L").Value, saveInFolder & .Cells(r, "M").Value & ".jpg"
        Next
    End With
Remove the & ".jpg" if the file names in column M the file extension (e.g. ".jpg"), and change the saveInFolder as required.

NB back slashes are missing from the linked code. The saveInFolder lines should be:
Code:
    saveInFolder = ThisWorkbook.Path & "\"
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
 
Last edited:
Upvote 0
Try adapting the code at https://www.mrexcel.com/forum/excel...tead-excel-sheet-post5080413.html#post5080413

One change, to use the links in column L and save them as the names in column M:

Code:
    With ActiveSheet
        lr = .Cells(Rows.Count, "L").End(xlUp).row
        For r = 2 To lr
            DownloadFile .Cells(r, "L").Value, saveInFolder & .Cells(r, "M").Value & ".jpg"
        Next
    End With
Remove the & ".jpg" if the file names in column M the file extension (e.g. ".jpg"), and change the saveInFolder as required.

NB back slashes are missing from the linked code. The saveInFolder lines should be:
Code:
    saveInFolder = ThisWorkbook.Path & "\"
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"


Code:
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  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 Long
Private Declare PtrSafe Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias "DeleteUrlCacheEntryA" _
    (ByVal lpszUrlName As String) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
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
Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias "DeleteUrlCacheEntryA" _
    (ByVal lpszUrlName As String) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If


Private Const BINDF_GETNEWESTVERSION As Long = &H10




Public Sub Download_Images()
    
    Dim lr As Long, r As Long
    Dim saveInFolder As String
    
    
    
    saveInFolder = ThisWorkbook.Path & ""
    If Right(saveInFolder, 1) <> "" Then saveInFolder = saveInFolder & ""
    
    
    With ActiveSheet
        lr = .Cells(Rows.Count, "L").End(xlUp).Row
        For r = 2 To lr
            DownloadFile .Cells(r, "L").Value, saveInFolder & .Cells(r, "M").Value & ".jpg"
        Next
    End With
    
End Sub




Private Function DownloadFile(URL As String, LocalFilename As String) As Boolean
    
    Dim retVal As Long
    
    DeleteUrlCacheEntry URL
    retVal = URLDownloadToFile(0, URL, LocalFilename, BINDF_GETNEWESTVERSION, 0)
    If retVal = 0 Then DownloadFile = True Else DownloadFile = False


End Function

I tried to run this code but it's not doing anything i also dont know where to put the saving path i want it to be saved on c:\a
 
Upvote 0

Forum statistics

Threads
1,224,903
Messages
6,181,658
Members
453,059
Latest member
jkevin

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