我在做一个网络资源管理自动化项目。代码从网站下载并保存文件。可以使用“SendKeys”下载和保存文件,但这不是一个可靠的方法,因为我无法检测到下载通知:

有没有一种没有“SendKeys”的下载和保存文件的方法?或者至少有一种方法可以检测到这个通知的存在吗?
Ps -我参考了这些链接,这些链接对IE8下载很有帮助:
有什么帮助吗?
发布于 2015-06-19 15:49:58
为什么要使用SendKeys方法下载文件?对不起,这是错误的做法。使用API!
解决方案1:
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
Sub DownloadFilefromWeb()
Dim strSavePath As String
Dim URL As String, ext As String
Dim buf, ret As Long
URL = Worksheets("References & Resources").Range("URLMSL").Value
buf = Split(URL, ".")
ext = buf(UBound(buf))
strSavePath = ThisWorkbook.Path & "\" & "DownloadedFile." & ext
ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
If ret = 0 Then
MsgBox "Download has been succeed!"
Else
MsgBox "Error"
End If
End Sub来源:VBA -从网站保存文件
解决方案2:
Option Explicit
Sub DownloadXLFileFromURL()
Dim myURL As String, sFilename As String
myURL = "http://img.chandoo.org/hw/max-change-problem.xlsx"
sFilename = Environ("SystemDrive") & Environ("HomePath") & _
Application.PathSeparator & "Desktop" & Application.PathSeparator & _
"file.xlsx"
Dim WinHttpReq As Object, oStream As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False ', "username", "password"
WinHttpReq.Send
myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.ResponseBody
oStream.SaveToFile sFilename, 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
End Sub解决方案3:
最后,您可以创建自定义类,它可以创建MS的一个实例。现在,您可以通过使用IE对象的属性和事件(如DownloadBegin、DownloadComplete、FileDownload )来控制/管理IE对象。
注意:在我试过之前.
https://stackoverflow.com/questions/27638129
复制相似问题