Sub DownloadPDFs()
Dim ws As Worksheet
Dim urlRange As Range
Dim cell As Range
Dim url As String
Dim fileName As String
Dim savePath As String
Dim http As Object
Dim fileStream As Object
Dim fileData As Variant
Dim response As Variant
' Set worksheet and range
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name if different
Set urlRange = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row)
' Set save path
savePath = "C:\Users\g1cxr07\OneDrive - FR Banks\JamesPDFs\"
' Initialize HTTP and FileStream objects
Set http = CreateObject("MSXML2.XMLHTTP")
Set fileStream = CreateObject("ADODB.Stream")
' Loop through each URL
For Each cell In urlRange
If cell.Value <> "" Then
url = cell.Value
fileName = Mid(url, InStrRev(url, "/") + 1)
On Error GoTo ErrorHandler
' Send HTTP request
http.Open "GET", url, False
http.send
If http.Status = 200 Then
' Write response to file
fileStream.Type = 1 ' adTypeBinary
fileStream.Open
fileStream.Write http.responseBody
fileStream.SaveToFile savePath & fileName, 2 ' adSaveCreateOverWrite
fileStream.Close
cell.Offset(0, 1).Value = "Downloaded"
Else
cell.Offset(0, 1).Value = "Error"
End If
On Error GoTo 0
End If
Next cell
' Clean up
Set http = Nothing
Set fileStream = Nothing
MsgBox "Download complete!"
Exit Sub
ErrorHandler:
cell.Offset(0, 1).Value = "Error"
Resume Next
End Sub
Dim ws As Worksheet
Dim urlRange As Range
Dim cell As Range
Dim url As String
Dim fileName As String
Dim savePath As String
Dim http As Object
Dim fileStream As Object
Dim fileData As Variant
Dim response As Variant
' Set worksheet and range
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name if different
Set urlRange = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row)
' Set save path
savePath = "C:\Users\g1cxr07\OneDrive - FR Banks\JamesPDFs\"
' Initialize HTTP and FileStream objects
Set http = CreateObject("MSXML2.XMLHTTP")
Set fileStream = CreateObject("ADODB.Stream")
' Loop through each URL
For Each cell In urlRange
If cell.Value <> "" Then
url = cell.Value
fileName = Mid(url, InStrRev(url, "/") + 1)
On Error GoTo ErrorHandler
' Send HTTP request
http.Open "GET", url, False
http.send
If http.Status = 200 Then
' Write response to file
fileStream.Type = 1 ' adTypeBinary
fileStream.Open
fileStream.Write http.responseBody
fileStream.SaveToFile savePath & fileName, 2 ' adSaveCreateOverWrite
fileStream.Close
cell.Offset(0, 1).Value = "Downloaded"
Else
cell.Offset(0, 1).Value = "Error"
End If
On Error GoTo 0
End If
Next cell
' Clean up
Set http = Nothing
Set fileStream = Nothing
MsgBox "Download complete!"
Exit Sub
ErrorHandler:
cell.Offset(0, 1).Value = "Error"
Resume Next
End Sub