thepepp
thepepp

Reputation: 304

VBA - WinHttpRequest.Send Progress

In my code I have the following line

Dim Http2 As New WinHttpRequest
Http2.Open "GET", URL2, False
Http2.Send

when the third line is executed, excel goes black and seems like it's not responding. Is there a way to get a status on the operation? Could I make a progress bar?

Upvotes: 1

Views: 416

Answers (2)

Brian M Stafford
Brian M Stafford

Reputation: 8868

If you declare the http2 variable WithEvents then you can do the following:

Option Explicit

Private WithEvents Http2 As WinHttpRequest
Private URL2 As String

Private Sub CommandButton1_Click()
   Set Http2 = New WinHttpRequest
   Http2.Open "GET", URL2, False
   Http2.Send
End Sub

Private Sub Http2_OnResponseStart(ByVal Status As Long, ByVal ContentType As String)
   ProgressBar1.Value = 0
   ProgressBar1.Max = CSng(Http2.GetResponseHeader("Content-Length"))
End Sub

Private Sub Http2_OnResponseDataAvailable(Data() As Byte)
   ProgressBar1.Value = ProgressBar1.Value + UBound(Data)
End Sub

If the "Content-Length" header isn't available, one option would be to play some sort of animation:

Private Sub Http2_OnResponseStart(ByVal Status As Long, ByVal ContentType As String)
   Animation1.Visible = True
   Animation1.Open "filecopy.avi"
   Animation1.Play
End Sub

Private Sub Http2_OnResponseFinished()
   Animation1.Stop
   Animation1.Close
   Animation1.Visible = False
End Sub

Upvotes: 0

thepepp
thepepp

Reputation: 304

Thanks for the answer but it does not work. There is no Content-Lenght header. here is the result of GetAllResponseHeaders

Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: Keep-Alive
Date: Tue, 12 May 2020 16:30:23 GMT
Keep-Alive: timeout=5, max=100
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: text/html;charset=utf-8
Expires: Tue, 12 May 2020 16:30:23 GMT
Server: Apache
Set-Cookie: JSESSIONID=Sa-cFWD2CHr2DjK0+KE6GH4r; Path=/pvp; Secure;HttpOnly;Secure
Set-Cookie: ROUTEIDPVP=.14; path=/pvp; HTTPOnly; Secure
Vary: Accept-Encoding
Strict-Transport-Security: max-age=63072000; includeSubdomains;
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY

also I had to write the code as Class Module

Upvotes: 1

Related Questions