VBA / .Net download files from password protected website via Windows Security

♀尐吖头ヾ 提交于 2020-01-14 04:42:23

问题


I have a SharePoint test website (Azure SharePoint farm) which contains several image files and some document templates. I want to download the images to my local drive. I anticipate looping through a list of URLs, e.g. http://mySharePointSite/Sites/Images/Image01.png and downloading each in turn. I've tried URLDownloadToFile which works great if I put the images on a regular website but I'm unable to get past Windows Security with this approach.

I had a go at creating an InternetExplorer.Application Object (late binding) and can view my images but am unable to download them. Was trying ieApp.ExecWB but this throws a Run Time Error that seems insurmountable.

I've also tried WinHTTP.WinHTTPrequest.5.1 (StackOverflow 22051960) but, while that looked promising it returned a short string "???????" instead of an image.

I'm really hoping for a VBA solution and am wondering if Impersonate User might provide options, or if there are other options (excluding using SendKeys). Sadly I'm pushing the limits of my VBA knowledge and could really use some guidance.

Is it possible to download from share point from VBA?
If so how, am I missing something simple?

I can paste code, but really I'm not sure I have anything worth sharing. If this is not possible or unreliable in VBA then would be possible with VB.Net (my next steepest learning curve)?


回答1:


Hope this helps someone. I ended up using Visual Studio to create a dll in VB.Net that I can call from VBA. VB.Net dll requires references to microsoft.sharepoint.client

And the VBA code requires a reference to the resulting dll.

Imports Microsoft.SharePoint.Client

Module Module1
    Sub Main()
        Dim myContext As Microsoft.SharePoint.Client.ClientContext
        myContext = New ClientContext("http://TestSP-a.cloudapp.net/sites/Images/")

        Dim myCredentials As New System.Net.NetworkCredential
        '' I'm accessing a website from the outside so there
        '' are no credentials on my PC. If on the same NW I 
        '' assume I can use the line below instead of the 
        '' 3 lines that follow.
        'myCredentials = System.Net.CredentialCache.DefaultNetworkCredentials
        myCredentials.UserName = "UserNameForSharePoint"
        myCredentials.Password = "PassWordForSharePoint"
        myCredentials.Domain = ""   ' <-- Leave Blank


        Dim mySiteSP As New Uri("http://TestSP-a.cloudapp.net/sites/Images/Image1.png")
        Dim myTemp As String = "C:\temp\test.png"
        My.Computer.Network.DownloadFile(mySiteSP, myTemp, myCredentials, True, 600000I, False)


    End Sub

End Module


来源:https://stackoverflow.com/questions/32697709/vba-net-download-files-from-password-protected-website-via-windows-security

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!