1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
Sub test()
Dim chemin As String
Dim linck$
chemin = Environ("userprofile") & "\DeskTop\monpdf.pdf"
telechargeFichier "https://aida.ineris.fr/liste_documents/1/18023/1", , linck
MsgBox "le dernier lien document de la page est " & vbCrLf & linck
If linck <> "" Then telechargeFichier "https://aida.ineris.fr/sites/default/files/gesdoc/30296/BrochureNom_v47public.pdf", chemin
End Sub
Sub telechargeFichier(url As String, Optional chemin As String = "", Optional linck)
Dim ReQ As Object, oStream As Object
'On Error Resume Next 'On ne gère pas les erreurs
Set ReQ = CreateObject("Microsoft.XMLHTTP")
ReQ.Open "get", url, False
ReQ.send
If chemin = "" Then
With CreateObject("htmlfile")
.body.innerhtml = ReQ.responsetext
For Each elem In .all
If elem.classname = "lien-document" Then linck = Replace(elem.ChildNodes(0).href, "about", "https://aida.ineris.fr") ': MsgBox linck
Next
End With
Else
If Dir(chemin) <> "" Then Kill chemin
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write ReQ.responsebody
oStream.SaveToFile chemin
oStream.Close
End If
End Sub |
Partager