vba download web file

Sub test()
    Dim H, S
    Set H = CreateObject("Microsoft.XMLHTTP")
    H.Open "GET", http://www.163.com/test.exe, False   '文件网址

    H.send   
    Set S = CreateObject("ADODB.Stream")
    S.Type = 1 '二进制
    S.Open
    S.write H.Responsebody  '写入取得的内容
    S.savetofile "c:\temp\test.exe", 2  '保存文档

    S.Close
End Sub


Sub test2()

Dim bt() as byte '建立数组

Dim H As Object
    Set H = CreateObject("Microsoft.XMLHTTP")
    H.Open "GET", "Http://www.163.com/test.exe", False
    H.send
    If H.Status = 200 Then '没有超时

        bt = H.Responsebody

        Open "http://www.163.com\test.exe" For Binary As #1 '建立二进制文件
        Put 1, , bt '写入文件
        Close #1
    End If
End Sub


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

'申明API


Sub downlaod()
    URLDownloadToFile 0, "http://www.163.com/test.exe", "c:\temp\ver.exe", 0, 0
End Sub



猜你喜欢

转载自tangleilei.iteye.com/blog/2307894