compile insert VBS_ASYNC_DOWNLOAD Option Explicit Dim fso : Set fso = CreateObject("Scripting.FileSystemObject") 'Default to text downloads Dim isBinaryDL : isBinaryDL = False 'Reference to the XMLHTTP Com object Dim objXMLHTTP 'The URL being requested Dim strURL 'This is the entry point for the wrapper. 'The method indicates the URL of the resource to download and 'a boolean value of 1 (True) or 0 (False) if the response should 'be treated as binary data. Sub DownloadAsync(url,isBinary) 'Create the COM object for handling the download Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") 'Normalize the input value. Assume text downloads if isBinary = 1 Then isBinaryDL = True Else isBinaryDL = False strURL = URL 'Load the XMLHTTP object and send the request Call objXMLHTTP.open("GET", url, true) objXMLHTTP.send() End Sub 'This function returns the current state of the download. For a complete list 'of possible integer return values see 'http://msdn.microsoft.com/en-us/library/ms753800(v=vs.85).aspx ' 'Returns 'err' if there was a fatal error ' Function getDownloadState() 'Assume function failed as default return getDownloadState = "err" 'Continue running code if .readyState fails On Error Resume Next getDownloadState = objXMLHTTP.readyState If Err.Number <> 0 Then 'If failed clear the error and return default value Err.Clear End If 'Re-set the error handler to halt the VBScript on future errors On Error Goto 0 'getDownloadState will either be 'err' or the .readyState value End Function 'This function returns the downloaded content. Text downloads are returned 'as a string while binary downloads are saved to a temporary file and the 'path is returned to the calling program. ' 'Returns an empty string if the HTTP status is not 200. ' Function getDownloadContent() if objXMLHTTP.status <> 200 Then getDownloadContent = "Response Status: " & objXMLHTTP.status Else if isBinaryDL = True Then 'Generate a temporary file to hold the downloaded content Dim strTempDLPath : strTempDLPath = GetTempFilePath() 'Create an ADODB stream object for saving the binary data Dim objADOStream : Set objADOStream = CreateObject("ADODB.Stream") objADOStream.Open objADOStream.Type = 1 'Write the downloaded contant into the stream objADOStream.Write objXMLHTTP.ResponseBody objADOStream.Position = 0 'Save the stream to the temporary file objADOStream.SaveToFile strTempDLPath objADOStream.Close Set objADOStream = Nothing 'Return the path to the temp file getDownloadContent = strTempDLPath Else 'Return the downloaded content as a string getDownloadContent = objXMLHTTP.ResponseText End If End If End Function ' 'Function returns the status code and status text of the download request. ' Function getDownloadResponse() getDownloadResponse = objXMLHTTP.status & " " & objXMLHTTP.statusText End Function Function getURL() getURL = strURL End Function ' 'Call this function if the user wants to end the download early. ' Function abortDownload() objXMLHTTP.abort End Function ' 'Helper function to return a temporary file path. ' Function GetTempFilePath() Dim tfolder : Set tfolder = fso.GetSpecialFolder(2) GetTempFilePath = tfolder & "\" & fso.GetTempName End Function Function GetTempPath() Dim tfolder : Set tfolder = fso.GetSpecialFolder(2) GetTempPath = tfolder End Function