Asynchronous File Downloads from Within VBA (Excel)

前端 未结 3 789
轮回少年
轮回少年 2020-12-15 00:30

I\'ve already tried using many different techniques with this... One that works pretty nicely but still ties up code when running is using the api call:

Priv         


        
3条回答
  •  無奈伤痛
    2020-12-15 01:25

    @TheFuzzyGiggler: +1: Thanks for sharing back. I know its an old post but perhaps I make someone happy with this addidion to TheFuzzyGigglers code (works only in classes):

    I added two properties:

    Private pCallBack as string
    Private pCallingObject as object
    
    Property Let Callback(ByVal CB_Function As String)
     pCallBack = CB_Function
    End Property
    
    Property Let CallingObject(set_me As Object)
     Set pCallbackObj = set_me
    End Property
    
    'and at the end of HTTP_OnResponseFinished()
    
    CallByName pCallbackObj, pCallback, VbMethod
    

    In my class I have

     Private EntryCollection As New Collection
    
     Private Sub Download(ByVal fromURL As String, ByVal toPath As String)
     Dim HTTPx As HTTPRequest
     Dim i As Integer
      Set HTTPx = New HTTPRequest
      HTTPx.SavePath = toPath
      HTTPx.Callback = "HTTPCallBack"
      HTTPx.CallingObject = Me
      HTTPx.Main fromURL
      pHTTPRequestCollection.Add HTTPx
    End Sub
    
    Sub HTTPCallBack()
    Dim HTTPx As HTTPRequest
    Dim i As Integer
    For i = pHTTPRequestCollection.Count To 1 Step -1
      If pHTTPRequestCollection.Item(i).RequestDone Then pHTTPRequestCollection.Remove i
    Next
    End Sub
    

    You could access the HTTP object from the HTTPCallBack and do many beautiful things here; the main thing is: its perfectly asynchronous now and easy to use. Hope this helps someone as the OP helped me.

    I developed this further into a class: check my blog

提交回复
热议问题