download a file via internet and save it locally
Hi,
I would like to have a script that download a file (a word document) from internet (http/https) and save it on local PC.
I've already found how to download a file:
************************************************************
Dim WinHttpReq
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
WinHttpReq.Open "GET", "http://", False
************************************************************
but then, I don't know how to save this file on my PC :(
Anyone has an ideea?
Thank you,
(Paul)
Per one of our support engineers:
This is the vba code to download a file from an URL, then save it to disk. The sample is save the the system temp folder, you can change the sImagePath to save the file to another folder. I tested and successfully download the file.
Sub DoIt()
Const adTypeBinary = 1
Const adSaveCreateNotExist = 1
Const TemporaryFolder = 2
Dim oStream ' ADODB.Stream
Dim oFSO ' Scripting.FileSystemObject
Dim xml ' Microsoft.XMLHTTP
Dim sURL
Dim oFil
Dim sImagePath
' create objects
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set xml = CreateObject("Microsoft.XMLHTTP")
Set oStream = CreateObject("Adodb.Stream")
sURL = "http://www.csdn.net/Images/logo_csdn.gif"
' get the temp file...
sImagePath = oFSO.GetSpecialFolder(TemporaryFolder) & "\" & oFSO.GetTempName
' sImagePath = "d:\logo_csdn.gif"
xml.Open "GET", sURL, False
xml.Send
oStream.Type = adTypeBinary
oStream.Open
oStream.write xml.responseBody
' Do not overwrite an existing file
oStream.savetofile sImagePath, adSaveCreateNotExist
oStream.Close
' grab reference to the file...
Set oFil = oFSO.GetFile(sImagePath)
ActiveDocument.Application.Selection.InlineShapes.AddPicture oFil.Path
' cleanup...
oFil.Delete
Set oStream = Nothing
Set xml = Nothing
Set oFSO = Nothing
Set oFil = Nothing
End Sub
I hope this helps,
-brenda (ISV Buddy Team)