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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| Declare Function OuvreInternet Lib "wininet" _
Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, _
ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Declare Function fermeInternet Lib "wininet" _
Alias "InternetCloseHandle" (ByVal hInet As Long) As Integer
Declare Function code_page Lib "wininet" _
Alias "InternetReadFile" (ByVal hFile As Long, ByVal sBuffer As String, _
ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Declare Function Ouvrepage Lib "wininet" _
Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, _
ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Long
Sub télécharge_http()
Dim texte_code As String * 1024
fich = InputBox("adresse Internet du fichier à télécharger ?", _
"téléchargement HTTP", "http://tonadresse/tonfichier.wav")
'recherche extension du fichier
extn = ""
txt = fich
encor:
extn = Right(txt, 1) & extn
txt = Left(txt, Len(txt) - 1)
If Left(extn, 1) <> "." Then GoTo encor
If extn = ".fr" Or extn = ".com" Then extn = ".html"
'connection au fichier à télécharger
internet = OuvreInternet("toto", 1, vbNullString, vbNullString, 0) 'ouvre Internet
URL = Ouvrepage(internet, fich, vbNullString, _
ByVal 0&, &H80000000, ByVal 0&) 'ouvre la page Web
'lecture du fichier par paquet de 1024 bytes
txt = ""
nb_caractères_lus = 1
Do While nb_caractères_lus > 0
code_page URL, texte_code, 1024, nb_caractères_lus
txt = txt & Left(texte_code, nb_caractères_lus)
Loop
'ménage
fermeInternet URL 'ferme la page
fermeInternet internet 'ferme Internet
'recopie dans un fichier
Open "c:\rien" & extn For Output As #1
Print #1, txt
Close #1
'ouverture du fichier téléchargé
ThisWorkbook.FollowHyperlink "c:\rien" & extn, , True
End Sub |
Partager