• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Download files from URL

Pegaso

New Member
Hello All,

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.

I had found solutions when the name of the file is known, but this is not my case, I hope somebody can help.

Warmest regards!

Pegaso
 
Hi Pegaso,

Pls share the solution you have when name of files are known. We can try to tweak the same to your requirements, rather than recreating some scratch.

Regards,
Prasad DN
 
Thanks Prasad,

I found the following thread in other famous excel-VBA user forum:

http://www.mrexcel.com/forum/excel-questions/353006-download-file-excel.html

Three codes are proposed there, the second one would cover my needs:

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

The info I need to download is password-protected, so I cannot give you the login info for you to take a look, but below is a screen-capture. As you can see, each URL directs the user to a "vault" with few files in there (usually .pdf but also .doc or .gif). What I want is basically to replicate the content of each vault in my local HD. The code above doesn't work for my case, even if you input the file's location (you can know it after you click in the link, the little folder to the right), but since the files come in different formats, the dialog to save them is not always available (Just with the .pdf files seems to work fine), for the rest it would be required to select the file (ctrl+A), copy & paste it to a new document, and save it.

It is probably more complicate that I thought at the beginning, also I need to say that at the same time I posted the same question in other forums, (with no success by the way), should I include links to those here?

Warmest regards! - Pegaso
 

Attachments

  • Capture3.JPG
    Capture3.JPG
    97.6 KB · Views: 5
Back
Top