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.
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