badkatro
2/23/2016 - 6:36 PM

Function to download file to disk using MSXML2.XMLHTTP, Tested to work! Untangle the code, response body type, etc

Function to download file to disk using MSXML2.XMLHTTP, Tested to work! Untangle the code, response body type, etc

Sub DownloadFile()

Dim myURL As String
myURL = "https://github.com/badkatro/AssignDocuments/blob/master/scripts/Main.vba"

'Dim WinHttpReq As Object
Dim WinHttpReq As XMLHTTP
'Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
Set WinHttpReq = New XMLHTTP

WinHttpReq.Open "GET", myURL, False, "badkatro", "Kocutel924"
WinHttpReq.send

Do While WinHttpReq.readyState <> 4
    DoEvents
Loop

myURL = WinHttpReq.responseBody

If WinHttpReq.Status = 200 Then
    
    Dim fso As New Scripting.FileSystemObject
    
    Dim ostream As TextStream
    
    Set ostream = fso.CreateTextFile("D:\Main.bas", True)
    'Set oStream = CreateObject("ADODB.Stream")
    
    'oStream.Open
    ostream.write (WinHttpReq.responseText)
    'ostream.Type = 1
    'ostream.write WinHttpReq.responseText
    'ostream.SaveToFile "D:\Main.bas", 2 ' 1 = no overwrite, 2 = overwrite
    ostream.Close
End If

Set WinHttpReq = Nothing

End Sub