从在VBA(EXCEL)异步文件下载 [英] Asynchronous File Downloads from Within VBA (Excel)

查看:848
本文介绍了从在VBA(EXCEL)异步文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试使用多种不同的技术这个......那一个很好的工作pretty但运行使用API​​调用时仍然占用了code:

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:

Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long

IF URLDownloadToFile(0, "URL", "FilePath", 0, 0) Then
End If

我也用(成功)code到在Excel中编写VBScript,然后运行它WScript的,等待回调。但同样,这不是完全异步,仍然占用了一些code的。

I've also used (Successfully) code to write vbscript from within Excel and then running with it wscript and waiting for the callback. But again this isn't totally async and still ties up some of the code.

我想有文件下载事件驱动类和VBA code可以在一个大循环使用的DoEvents做其他事情。当一个文件被做了它可以触发一个标志和code可以处理文件,而等待另一个。

I'd like to have the files download in an event driven class and the VBA code can do other things in a big loop with "DoEvents". When one file is done it can trigger a flag and the code can process that file while waiting for another.

这是拉Excel文件了一个内部​​网站。如果没有什么帮助。

This is pulling excel files off of an Intranet site. If that helps.

既然我敢肯定,有人会问,我无法用任何东西,但VBA。这将在工作场所中使用和计算机90%的共享。我很怀疑他们会为春天让我的Visual Studio无论是业务费用。所以,我有与我有什么工作。

Since I'm sure someone will ask, I can't use anything but VBA. This is going to be used at the workplace and 90% of the computers are shared. I highly doubt they'll spring for the business expense of getting me Visual Studio either. So I have to work with what I have.

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

您可以做到这一点使用XMLHTTP在异步模式,一个类来处理它的事件:

You can do this using xmlhttp in asynchronous mode and a class to handle its events:

<一个href=\"http://www.dailydoseofexcel.com/archives/2006/10/09/async-xmlhttp-calls/\">http://www.dailydoseofexcel.com/archives/2006/10/09/async-xmlhttp-calls/

在code有解决responseText的,但你可以调整使用.responseBody。这里有一个(同步)例如:

The code there is addressing responseText, but you can adjust that to use .responseBody. Here's a (synchronous) example:

Sub FetchFile(sURL As String, sPath)
 Dim oXHTTP As Object
 Dim oStream As Object


    Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
    Set oStream = CreateObject("ADODB.Stream")
    Application.StatusBar = "Fetching " & sURL & " as " & sPath
    oXHTTP.Open "GET", sURL, False
    oXHTTP.send
    With oStream
        .Type = 1 'adTypeBinary
        .Open
        .Write oXHTTP.responseBody
        .SaveToFile sPath, 2 'adSaveCreateOverWrite
        .Close
    End With
    Set oXHTTP = Nothing
    Set oStream = Nothing
    Application.StatusBar = False


End Sub

这篇关于从在VBA(EXCEL)异步文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆