vba URLDownloadToFile - 下载的文件中缺少数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12269941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URLDownloadToFile - Data missing in downloaded file
提问by user1647155
Using URLDownloadToFile
in VBA, I am trying to download a file. The problem is that a blank file is getting downloaded. Any idea why the data is missing?
URLDownloadToFile
在 VBA 中使用,我试图下载一个文件。问题是正在下载一个空白文件。知道为什么数据丢失吗?
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
回答by user2800636
I have a similar issue. I use the following code but got an "overflow" message:
我有一个类似的问题。我使用以下代码但收到“溢出”消息:
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
回答by Nirmal
you got an overflow because you used a integer to collect a long value. The urldownloadtofile returns a long value. If your download had succeeded you would have received a "0". Then your code would've worked.
你有一个溢出,因为你使用了一个整数来收集一个长值。urldownloadtofile 返回一个长值。如果您的下载成功,您将收到“0”。那么你的代码就可以工作了。