WebClient Object elements not showing in vba7 object browser

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

I was hoping this would be cakewalk, but for reasons beyond my control, I cannot view or use members of the WebClient class in the VBA 7 IDE. I already referenced the .Net 2 framework of System.tlb and can see the classes in the System.Net namespace, but the members for many classes are not visible, and I cannot use them in my code.

I get a compilation error when trying to use this code:

Dim Downloader as New System.WebClient

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

Perhaps I did not register the .Net classes that will be used in VBA and therefore the problem, but; System.dllmentioned in my project is inC:\Windows\Microsoft.NET\Framework\v2.0.50727\System.tlb

which bothers me even more. Also, it really helps if someone can describe the process of referencing the .Net Framework libraries in VBA 7.

Thanks in advance!

0
source share
1 answer

Instead of .Net libraries, I suggest using the MSXML library. You can add it to VBA in the IDE by clicking "Tools" ---> "Links ..." and checking the box next to "Microsoft XML, xx", where xx is the latest 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
+1
source

Source: https://habr.com/ru/post/1694857/


All Articles