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 !:):p
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,276
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:

sashaP

New Member
Joined
Jul 19, 2019
Messages
8
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
 

Watch MrExcel Video

Forum statistics

Threads
1,099,788
Messages
5,470,789
Members
406,721
Latest member
Laiceyshae

This Week's Hot Topics

Top