(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

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

                End Function

Leave a Reply

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