office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

VB及VBA下載網上文件代碼且有進度條顯示

2017-09-20 21:35:00
網絡未知
轉貼
5887
'添加 internet transfer control 6.0 和 windows commom controls 6.0
'form代碼:

Private Sub cmdGET_Click()
StartDownLoad txtURL
End Sub

Private Sub Form_Load()
savefile.Text = App.Path
End Sub

Private Sub StartDownLoad(ByVal Geturl As String)
Dim spo%, filename$
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(App.Path & "\download") Then Set f = fso.CreateFolder(App.Path & "\download")
spo = InStrRev(Geturl, "/")
filename = Right(Geturl, Len(Geturl) - spo) '穫取文件名
savefile.Text = App.Path & "\download" & filename
Inet1.Execute Geturl, "get"   '開始下載
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
'State = 12 時,用 GetChunk 方法檢索服務器的響應。
Dim vtData() As Byte
Select Case State
'...沒有列舉其牠情況。
Case icError '11
'齣現錯誤時,返迴 ResponseCode 和 ResponseInfo。
vtData = Inet1.ResponseCode & ":" & Inet1.ResponseInfo
Case icResponseCompleted ' 12
Dim bDone As Boolean: bDone = False
'取得第一箇塊。
vtData() = Inet1.GetChunk(1024, 1)
DoEvents
Open savefile.Text For Binary Access Write As #1     '設置保存路徑文件後開始保存
'穫取下載文件長度
If Len(Inet1.GetHeader("Content-Length")) > 0 Then ProgressBar1.Max = CLng(Inet1.GetHeader("Content-Length"))

'循環分塊下載
Do While Not bDone
Put #1, Loc(1) + 1, vtData()
vtData() = Inet1.GetChunk(1024, 1)
DoEvents
ProgressBar1.Value = Loc(1)   '設置進度條長度
If Loc(1) >= ProgressBar1.Max Then bDone = True
Loop

Close #1
MsgBox "下載完成", vbInformation, "通知"
End Select

End Sub
分享