VBA:Internet Explorer控制下载

时间:2017-06-17 14:25:53

标签: vba internet-explorer access-vba

有没有人知道使用InternetExplorer.Application DownloadBegin使用FileDownload Event的方式以及可能的方法?我正在尝试检测IE何时下载文件,以便在文件下载完成后自动处理文件。

有一个DownloadComplete和一个{{1}}个活动,但这个问题在谈到导航到网址而不是精确的文件下载时。

1 个答案:

答案 0 :(得分:0)

您应该能够确认下载过程的状态。

Option Explicit
Declare PtrSafe 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

Sub DownloadFileAPI()
Dim strURL As String
Dim LocalFilePath As String
Dim DownloadStatus As Long

    strURL = "http://data.iana.org/TLD/tlds-alpha-by-domain.txt"
    LocalFilePath = "C:\Test\TEST2_tlds-alpha-by-domain.txt"
    DownloadStatus = URLDownloadToFile(0, strURL, LocalFilePath, 0, 0)
    If DownloadStatus = 0 Then
        MsgBox "File Downloaded. Check in this path: " & LocalFilePath
    Else
        MsgBox "Download File Process Failed"
    End If
End Sub


Sub DownloadFile()
Dim WinHttpReq As Object
Dim oStream As Object
Dim myURL As String
Dim LocalFilePath As String

myURL = "http://data.iana.org/TLD/tlds-alpha-by-domain.txt"
LocalFilePath = "C:\Test\TEST_tlds-alpha-by-domain.txt"

Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False, "", ""  '("username", "password")
WinHttpReq.send

If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.responseBody
    oStream.SaveToFile LocalFilePath, 2 ' 1 = no overwrite, 2 = overwrite
    oStream.Close
End If
End Sub