单击超链接后下载文件

时间:2019-08-08 13:59:02

标签: excel vba

enter image description here单击此超链接后,Internet Explorer将弹出,然后自动下载excel文件。如何获得自动下载文件然后保存到文件夹的信息?

我现在将网页保存到文件夹中,而不是下载的文件中。

Sub SaveSpreads()


Dim myURL As String
myURL = "https://www.citivelocity.com/analytics/chartingreport?user=cfok&dist=excel&jobid=352106&runid=1919430&includeCharts=true&hash=b5136b4028"

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
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 ("C:\Users\U708699\Desktop\Citi\CitiSpreads.csv")
oStream.Close
End If

End Sub

1 个答案:

答案 0 :(得分:0)

您可以尝试使用

 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 test()
    DownloadFile$ = "batchReport.xls" 'here the name with extension from 
    URL$ = "https://www.citivelocity.com/analytics/chartingreport?user=cfok&dist=excel&jobid=352106&runid=1919430&includeCharts=true&hash=b5136b4028" & DownloadFile 'Here is the web address
    LocalFilename$ = "C:\Users\U708699\Desktop\Citi\CitiSpreads.csv" 'here the drive and download directory
    MsgBox "Download Status : " & URLDownloadToFile(0, URL, LocalFilename, 0, 0) = 0
End Sub

添加对Microsoft WinHTTP Services的引用。在您的代码中,您可以更改URL以直接定位excel文件。请检查网页中URL后面的代码。