問題

我在幾種語言中找到了許多這樣做的例子,但沒有一個具體的VBA.這個問題,如何在VB6中下載具有進度條的多個檔案?,解決了在VB6中執行此操作的三種不同方法.

  1. 使用VB6 UserControl / UserDocument物件的ASyncRead屬性
  2. 使用型別庫olelib.tlb和IBindStatusCallback介面
  3. 使用 wininet.dll 將自己的下載寫入檔案函式

這些方法都不適用於我,因為:

  1. VBA不可用UserControl / UserDocument物件
  2. 我寧願不必維護和分發大型外部依賴項
  3. 我沒有看到獲取當前檔案下載進度的明顯方法

上面的數字2似乎最有希望.我想知道我是否可以使用我的VBA專案中的類模組建立一個IBindStatusCallback介面?

或者可能有可用的屬性/方法使用上面的Number 3提供當前進度.非常感謝任何幫助.

  最佳答案

我已經使用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
 

如果您需要任何澄清,請發表評論。

  相同標籤的其他問題

vbadownloadaccess-vba