(Solved) – VBA download form URL with login that redirect to another one

(solved)-–-vba-download-form-url-with-login-that-redirect-to-another-one

the website requires login then redirect you to other url then download the file

this function works fine with url with no redirction but not with my case

Function DownloadFile(URL As String, Path As String, UserName As String, Password As String) As Boolean

                    DownloadFile = False

                    Dim WinHttpReq As Object
                    Set WinHttpReq = CreateObject("MSXML2.ServerXMLHTTP.6.0")
                    WinHttpReq.Open "GET", URL, False, UserName, Password
                    WinHttpReq.send

                    Set oStream = CreateObject("ADODB.Stream")
                    oStream.Open
                    oStream.Type = 1
                    oStream.Write WinHttpReq.responseBody
                    oStream.SaveToFile Path, 2
                    oStream.Close
                    DownloadFile = True


                End Function

Leave a Reply

Your email address will not be published. Required fields are marked *