• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA to open and save excel file from authenticated(https) url in ie web page

kiruthika

New Member
Hi,

I need to download excel workbook from url and save it in local drive. Send keys not working for me. same time after downloading the excel contain login page image while opening. Is there any way to download?
 
Hi,

I need to download excel workbook from url and save it in local drive. Send keys not working for me. same time after downloading the excel contain login page image while opening. Is there any way to download?

This is my code which shows login page source code in downloaded workbook

Code:
Sub TestDown()
DownloadFile "url", "folderpath"
End Sub

Function DownloadFile(myURL As String, saveToPath As String)
Dim WinHttpReq As Object
Dim iTimer As Long
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False, "username", "password"
Debug.Print WinHttpReq.readyState
WinHttpReq.Send
'make sure readystate is finished
iTimer = Timer
Do While WinHttpReq.readyState = 1
'if 10 seconds elapse and nothing happens, abort:
If Timer - iTimer > 10 Then Exit Do
Loop
'readystate 4 = all data received
If WinHttpReq.readyState = 4 Then
    If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.ResponseBody
    oStream.SaveToFile saveToPath, 2 ' 1 = no overwrite, 2 = overwrite
    oStream.Close
    MsgBox ("Download Complete")
    Else
    MsgBox ("HTTP error: " & WinHttpReq.Status)
   End If
Else
MsgBox ("Couldn't get file")
End If
Set oStream = Nothing
Set WinHttpReq = Nothing
End Function
 
Back
Top