title: VBA URLDownloadToFileA or URLOpenStreamA from URLMON - Asynchronously
body: <p>I'm using URLDownloadToFileA API call to download files in VBA, which works fine (and is <em>MUCH</em> faster than WinHTTP or XMLHTTP), but it's synchronous only.</p>
  
  <p>I've been searching for ways to use a URLMon API call asynchronously (perhaps using URLOpenStream instead of download-to-file), but haven't figured out a way to do this.</p>
  
  <p>I stumbled upon VB6 code that might be able to do it here: <a href="http://www.mvps.org/emorcillo/download/vb6/adl.zip" rel="nofollow noreferrer">http://www.mvps.org/emorcillo/download/vb6/adl.zip</a> but I am not versed enough in coding to convert this to working VBA.</p>
  
  <p><strong>Please note:</strong> I do realize how to do this through XMLHTTP and WinHTTP with a class, but those are significantly slower than using the URLMon DLL API, so am hoping to find a solution there.</p>
  
  <p>Code to do it synchronously with URLMon: </p>
  
  <pre><code>Private Declare PtrSafe Function URLDownloadToFileA Lib "URLMON" (ByVal pcaller As Long, ByVal szurl As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As LongPtr
  Private Declare PtrSafe Function URLOpenPullStreamA Lib "URLMON" (ByVal pcaller As Long, ByVal szurl As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As LongPtr
  
  Sub test()
  dim a&amp;, b&amp;, URL$
  
  URL = "http://ipv4.download.thinkbroadband.com/100MB.zip"
  a = URLOpenPullStreamA (0, URL, 0, 0)
  b = URLDownloadToFileA(0, URL, "c:\testfiles\100MB.zip", 0, 0)
  
  End Sub
  </code></pre>
  
  <p>So, this works - but I don't know how to capture the callback for a, and while downloading b it locks excel until the file is fully downloaded.</p>
  
  <p>Any help would be greatly appreciated!</p>