0

I need to download and save a file automatically from a URI. I think I can use URLDownloadToFile from "urlmon" library but I wanted to use the WebClient.DownloadFile method.

I hoped it would be a cakewalk but for reasons beyond me, I am not able to view or use the members of the WebClient class in the VBA 7 IDE. I have already referenced the .Net 2 framework's System.tlb and am able to see the classes in the System.Net namespace but members for many of the classes are not visible and I cannot use them in my code.

I get a compilation error on trying to use a code like:

Dim Downloader as New System.WebClient

Downloader.DownloadFile("uri","filename")

Maybe I have not registered the .Net classes to be used in VBA and hence the problem but; the System.dll being referred to in my project is located at C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.tlb

which confuses me even more. Also, it will really help if someone can detail out the process to reference .Net Framework libraries in VBA 7.

Thanks in advance!

Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
user3006045
  • 1
  • 1
  • 1
  • If you want to use the .Net library in your VBA project, [it appears to be a bit more complicated than that](http://stackoverflow.com/questions/2378542/net-object-from-vb6-without-use-of-regasm-exe?lq=1). – Blackhawk Nov 18 '13 at 20:18
  • @Blackhawk: Thanks for posting that link :) That link just got deleted from the link that i posted :) – Siddharth Rout Nov 18 '13 at 20:28

1 Answers1

2

In lieu of .Net libraries, I suggest using the MSXML library. You can add it to VBA in the IDE by clicking "Tools" ---> "References..." and checking the box next to "Microsoft XML, x.x" where x.x is the most recent version.

Here is a quick test you can run:

Public Sub Downl()
    Dim xhttp As MSXML2.XMLHTTP
    Set xhttp = New MSXML2.XMLHTTP

    Dim oFile As Integer
    oFile = FreeFile()
    Open CurrentProject.Path & "\test.png" For Binary Access Write As oFile

    Dim bdata() As Byte

    With xhttp
        .Open "GET", "https://www.google.com/images/srpr/logo11w.png", False
        .send
        bdata = .responseBody
        Put oFile, 1, bdata
    End With

    Close oFile
End Sub
Blackhawk
  • 5,984
  • 4
  • 27
  • 56