我发现了很多用几种语言来做这个的例子,但没有一个是VBA特有的。这个问题How to download multiple files in VB6 with progress bar?解决了在VB6中执行此操作的三种不同方法。
这些方法都不适合我,因为:
上面的第2号似乎是最有希望的。我想知道我是否可以在我的VBA项目中使用类模块创建IBindStatusCallback接口?
或者可能使用上面的数字3提供可用于提供当前进度的属性/方法。非常感谢任何帮助。
答案 0 :(得分:2)
我使用wininet.dll函数完成了这项工作。不幸的是,我无法粘贴我的雇主拥有的代码。
使用InternetOpen和InternetOpenUrl开始下载,HttpQueryInfoLong获取内容长度然后重复调用InternetReadFile将数据读入缓冲区(我使用128k缓冲区),将数据写入文件并更新进度条去。
让你开始的宣言:
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function HttpQueryInfo Lib "wininet.dll" Alias "HttpQueryInfoA" (ByVal hHttpRequest As Long, ByVal lInfoLevel As Long, ByRef sBuffer As Any, ByRef lBufferLength As Long, ByRef lIndex As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByRef Buffer As Any, ByVal lNumberOfBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternet As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000 ' use keep-alive semantics - required for NTLM proxy authentication
Private Const HTTP_QUERY_CONTENT_LENGTH = 5
Private Const HTTP_QUERY_FLAG_NUMBER = &H20000000
如果您需要任何澄清,请发表评论。
答案 1 :(得分:0)
您想要在VBA中使用进度条,这些方法中的其中一种是否有效?
看起来比你描述的要简单得多,或者我不理解?
好的,试试这个。从URL获取标头并解析它们的Content-Length。然后,您可以相应地设置进度条。
Function GetFileSize(URL As String) As Long
Dim xml As Object ' MSXML2.XMLHTTP60
Dim result As String
Set xml = CreateObject("MSXML2.XMLHTTP.6.0")
With xml
' get headers only
.Open "HEAD", URL, False
.send
End With
result = xml.getResponseHeader("Content-Length")
GetFileSize = CLng(result)
End Function
现在只需使用您要下载的文件的URL调用该函数。它应该给你文件的字节数。