Need vba code for getting jpg from a file

Status
Not open for further replies.

krishna008

New Member
Joined
Jan 7, 2010
Messages
12
I got the url for each jpeg for an inline flip book . There are 300 jpegs meaning 300 urls. I will add them to column A of an excel sheet. Im new to vba could anyone help me with this vba code to open each URL from column A and download the jpeg file

Sub DownloadJPEGS()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim url As String, savePath As String

Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

For i = 1 To lastRow
url = ws.Cells(i, 1).Value
savePath = "C:\Your\Desired\Save\Path\File" & i & ".jpg" ' Change the save path accordingly

With CreateObject("MSXML2.ServerXMLHTTP")
.Open "GET", url, False
.send
If .Status = 200 Then
Open savePath For Binary Access Write As #1
Put #1, , .responseBody
Close #1
Debug.Print "File " & i & " downloaded successfully."
Else
Debug.Print "Error downloading file " & i & "."
End If
End With
Next i
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I tried this also

Option Explicit

#If VBA7 Then
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
#Else
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

Sub DownloadFilesFromURLs()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim url As String, savePath As String

' Set the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name

' Find the last row with data in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

' Loop through each URL in column A
For i = 1 To lastRow
url = ws.Cells(i, 1).Value
savePath = "C:\Your\Desired\Save\Path\" & "File" & i & ".jpg" ' Change the save path accordingly

' Download the file
If URLDownloadToFile(0, url, savePath, 0, 0) = 0 Then
' File downloaded successfully
Debug.Print "File " & i & " downloaded successfully."
Else
' Error downloading the file
Debug.Print "Error downloading file " & i & "."
End If
Next i
End Sub
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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