maximladus
maximladus

Reputation: 157

Download / Save file from Web Site / Web Page

I need to download the PDF files from the link below for the first/top 5 dates and save them on Desktop for instance. I have no clue how to start but also couldn't find something explicit on Google.

Do you think you can help me?

http://cetatenie.just.ro/ordine/articol-11/

Upvotes: 0

Views: 9572

Answers (1)

user2185045
user2185045

Reputation: 106

I would use Internet Explorer, and automate it using an SHDocVw.InternetExplorer object (VBA reference 'Microsoft Internet Controls', ieframe.dll).

You can either (a) create a new Internet Explorer window using Set x = New SHDocVw.InternetExplorer or (b) acquire an existing Internet Explorer window using Set owins = CreateObject("Shell.Application").Windows (owins is an array, loop through it until you find one where Mid(TypeName(owins(i).Document), 1, 12) = "HTMLDocument").

Once you have an Internet Explorer ie, you can call ie.Navigate(url) to go to a website.

To wait for Internet Explorer to finish navigating before you interrogate it, you can run something like:

Do While mascot_win.Busy
    Application.Wait DateAdd("s", 1, Now)
    DoEvents
Loop

To get the URLs for the first five PDFs on that page, you'd need to examine the HTML of the page. There are two ways, depending on how well-formed the HTML is. If the HTML is well-written, then you can navigate the Document Object Model (the tags, like XML) with ie.Document.all(). But if the HTML is not well-formed, you may have to resort to reading the HTML from ie.Document.all(0).innerHTML.

By the looks of the link you gave, you will be looking for things like:

<li>Data de <strong>22.03.2013</strong>, numarul: <a href="/wp-content/uploads/Ordin-149P-din-22.03.2013.pdf">149P</a></li>

Once you have isolated each PDF URL (using either the attribute of the <a> tag in the DOM model or using lots of Mid() calls on the HTML), you can download it using:

Private 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

Dim ss As String
Dim ts As String
ss = "http://blah/blah/blah.pdf"
ts = "c:\meh\blah.pdf"
URLDownloadToFile 0, ss, ts, 0, 0

Upvotes: 2

Related Questions