Télécharger le contenu d'une page HTML

Comment télécharger depuis excel une page web sur mon PC ?

pour télécharger le fichier html :

1) méthode rapide mais pas très souple

 Declare Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String)
 As Long
 
 sub telecharge()
 DoFileDownload
 StrConv("http://jacxl.free.fr/cours_xl/ouvrir_web_suite.html", vbUnicode)
 end sub

2) méthode plus souple mais plus lourde

 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 = "http://jacxl.free.fr/cours_xl/ouvrir_web_suite.html"
 
 'connection au fichier à télécharger
 internet = OuvreInternet("", 1, vbNullString, vbNullString, 0) 'ouvre
 Internet
 'URL = Ouvrepage(internet, fich, vbNullString, ByVal 0&, &H80000000, ByVal
 0&) 'ouvre la page Web
 URL = Ouvrepage(internet, fich, "", 0, 0, 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:\
 ien.html" For Output As #1
 Print #1, txt
 Close #1
 
 end sub

Auteur :

Mots clefs associés à cette page : ,