Excel VBA download image from url

Aeroman

New Member
Joined
Jun 26, 2018
Messages
1
Hello! I've made a script that uses IE to search for a product on a site and, if found, downloads its image.

The issue is the extension. It downloads the images but I can't figure out how to get it to add the extension of the original URL instead of having it user specified, which makes some images corrupt and unusable.

Sheet cell format is as follows :

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD="align: center"]Image Output Name[/TD]
[TD="align: center"]Search Term[/TD]
[TD="align: center"]Status[/TD]
[/TR]
[TR]
[TD="align: center"]Example[/TD]
[TD="align: center"]5202888105913[/TD]
[TD="align: center"]File Successfully Downloaded[/TD]
[/TR]
</tbody>[/TABLE]

The user can pick a file extension explicitly so the filename would be named "Example.png" / "Example.jpg"

Any other suggestion to further better the code in general is also welcome since I am a beginner in VBA.

Code:
Option ExplicitPrivate 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

Sub ImageDownload()
    Dim sheet As Worksheet
    Dim lastRow As Long
    Dim fileFormat As String
    Dim ie As New InternetExplorer
    Dim folderName As String
    Dim i As Long
    Dim imgName As String
    Dim doc As HTMLDocument
    Dim imgUrl As String
    Dim dlFunc As Long


    Set sheet = ActiveWorkbook.ActiveSheet
    
    sheet.Range("D1").ClearContents
    sheet.Range("C2", Cells(Rows.Count, Columns.Count)).ClearContents
    
    lastRow = sheet.Range("A" & Rows.Count).End(xlUp).Row
    
    fileFormat = InputBox("Please select file format. (Ex png, jpg, jpeg)")
    
    If fileFormat <> "png" And fileFormat <> "jpg" And fileFormat <> "jpeg" Then
        ie.Quit
        Exit Sub
    End If
    
    ie.Visible = False
    
    With Application.FileDialog(msoFileDialogFolderPicker)
    
        .Title = "Select a folder"
        .ButtonName = "Select"
        
        If .Show = -1 Then
            folderName = .SelectedItems(1)
        Else
            ie.Quit
            Exit Sub
        End If
        
    End With
    
    For i = 2 To lastRow
        ie.Navigate "https://www.skroutz.gr/search?keyphrase=" & sheet.Range("B" & i).Value
        
        Do
            DoEvents
        Loop Until ie.readyState = READYSTATE_COMPLETE
        
        Set doc = ie.document
        
        On Error Resume Next
        imgUrl = doc.querySelector("a[data-image-url]").toString
        If Err.Number = 91 Then
            sheet.Range("C" & i).Value = "Not Found"
        Else
            imgName = folderName & "\" & sheet.Range("A" & i).Value & "." & fileFormat
            dlFunc = URLDownloadToFile(0, imgUrl, imgName, 0, 0)
            If dlFunc = 0 Then
                sheet.Range("C" & i).Value = "File successfully downloaded"
            Else
                sheet.Range("C" & i).Value = "Unable to download the file"
            End If
        End If
    Next i
    
    sheet.Range("D1").Value = "Script Complete"
    ie.Quit
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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