Search code examples
vbaadodbwinhttp

VBA winhttp equivalent for local file


So I currently have a sub which copies documents stored on our intranet and saves them to our shared drive. Now, I would like to do the exact same thing, but with a file stored locally on a users computer, not on the intranet. How could I do that?

Sub intranetPullFile(link, extent, strFile)    
 Set WinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
 If WinHttp Is Nothing Then Set WinHttp = CreateObject("WinHttp.WinHttpRequest")
 WinHttp.Open "GET", link , False
 WinHttp.Send
 arrResponse = WinHttp.ResponseBody
 Set WinHttp = Nothing
 Set oADO = CreateObject("ADODB.Stream")
 oADO.Type = 1
 oADO.Open
 oADO.Write arrResponse
 oADO.SaveToFile strFile, 2
 oADO.Close
end sub

I tried just using this as is with their C:.... address being the link for the file, but I get an error: The URL does not use a recognized protocol.

So, I marked an answer cause it got what I needed done, THANKS! --- still curious if there is a 'local file equivalent' for how that winhttp works, but I can move on now, so im good. Thanks again.


Solution

  • You can copy and specify a new name with CopyFile(). It can also overwrite an existing file if you pass True as the 3rd param:

    With CreateObject("Scripting.FileSystemObject")
        .CopyFile strSourceFile, strDestFile, True  
    End With