2

在 VBA 中使用URLDownloadToFile,我正在尝试下载文件。问题是正在下载一个空白文件。知道为什么数据丢失了吗?

Option Explicit 

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 

Dim Ret As Long 

Sub Sample()

Dim strURL As String 
Dim strPath As String 

strURL = "https://abc.abcabc.com/cmif-ku/reports/2012/byOwningEntity/Excel/myfilename.xls" 

strPath = "C:\Temp\myfilename.xls" 

Ret = URLDownloadToFile(0, strURL, strPath, 0, 0) 

If Ret = 0 Then 
    MsgBox "File successfully downloaded" 
Else 
    MsgBox "Unable to download the file" 
End If

End Sub
4

2 回答 2

2

我有一个类似的问题。我使用以下代码,但收到“溢出”消息:

Sub downloadFile()
    Dim targetFile As String, targetUrl As String, returnVal As Integer
    target = "http://www.ishares.com/us/products/239454/ishares-20-year-treasury-bond-etf/1395165510757.ajax?fileType=xls&fileName=iShares-20-Year-Treasury-Bond-ETF"
    strSavePath = "C:\testdownload.txt"
    returnVal = URLDownloadToFile(0, target, strSavePath, 0, 0)
    If returnVal = 0 Then
        Debug.Print "Download ok!"
    Else
        Debug.Print "Error"
    End If
End Sub
于 2014-10-24T13:15:14.510 回答
1

你有一个溢出,因为你使用一个整数来收集一个长值。urldownloadtofile 返回一个长值。如果您的下载成功,您将收到“0”。那么你的代码就可以工作了。

于 2016-09-10T18:25:40.993 回答