0

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.

mrlemmer11
  • 347
  • 2
  • 15
  • FileSystemObject: http://stackoverflow.com/search?q=filesystemobject+vba – Don Jewett Aug 13 '15 at 20:04
  • I would use FSO, but I want to rename the local document to something else when I paste it into the shared drive. Since I already had this sub which allowed me to send in a different name for the file to be saved as I thought this would be a better way to go. I don't believe the .CopyFile of FSO allows me to rename does it? – mrlemmer11 Aug 13 '15 at 20:06
  • http://stackoverflow.com/questions/18966991/how-can-i-use-the-filesystemobject-to-copy-and-rename – Tim Williams Aug 13 '15 at 20:13
  • http://www.get-digital-help.com/2013/04/19/copyrename-a-file-excel-vba/ – Tim Williams Aug 13 '15 at 20:14

2 Answers2

0

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
Bond
  • 16,071
  • 6
  • 30
  • 53
0

VBA can do that in one line without FSO:

FileCopy fullSourcePathandName, fullDestinationPathandName
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • type mismatch: 'FileCopy' --- I cstr()'d both variables and still had mismatched errors. gonna try and hardcode source and dest locations to see if it's a variable issue or the filecopy itself. – mrlemmer11 Aug 13 '15 at 20:25