Download files from URL, unknown file names
[COLOR=rgba(0, 0, 0, 0.8)]Hello All,[/COLOR]
[COLOR=rgba(0, 0, 0, 0.8)]I have a list of URLs that each one opens a webpage were an undetermined number of files (.pdf) are stored, I cannot anticipate the names of the files, so I need a macro to go through each of those pages and automatically download all the files present there, save them in a local folder.[/COLOR]
[COLOR=rgba(0, 0, 0, 0.8)]I had found solutions when the name of the file is known, but this is not my case, I hope somebody can help.[/COLOR]
[COLOR=rgba(0, 0, 0, 0.8)]Warmest regards![/COLOR]
[COLOR=rgba(0, 0, 0, 0.8)]Pegaso[/COLOR]
Download files from URL - Code & clarifications
Here it goes the code in brackets, but before I want to clarify this:
I DIDN'T WORK THIS CODE, IT WAS AVAILABLE IN ANOTHER FORUM, Mr. EXCEL 353006, THANKS TO THE AUTHORS FOR PROVIDING IT
THE CODE WORKED ON A DIFFERENT URL THAN THE ONE THAT IT IS INTENDED TO WORK NOW, AS LONG AS THE COMPLETE URL OF THE FILE IS KNOWN (WHICH IS NOT MY CASE)
I may had found a solution to get all the links displayed in a URL, but still testing, if I make that work, I should be able to use the code below.
Code:
Sub Test2()
Dim i As Long
Dim FileNum As Long
Dim FileData() As Byte
Dim MyFile As String
Dim WHTTP As Object
On Error Resume Next
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
End If
On Error GoTo 0
If Dir("C:\MyDownloads", vbDirectory) = Empty Then MkDir "C:\MyDownloads"
For i = 1 To 10
MyFile = Cells(i, 1).Text
TempFile = Right(MyFile, InStr(1, StrReverse(MyFile), "/") - 1)
WHTTP.Open "GET", MyFile, False
WHTTP.Send
FileData = WHTTP.ResponseBody
FileNum = FreeFile
Open "C:\MyDownloads\" & TempFile For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum
Next
Set WHTTP = Nothing
MsgBox "Open the folder [ C:\MyDownloads ] for the downloaded file..."
End Sub