简体   繁体   中英

excel vba - copy file from one folder to another but if file exists do not overwrite?

Can someone please show me a way of copying a file from one folder to another using vba but with a condition to say if file already exists do not overwrite?

here is my code:

If Target.Column = Range("BL1").Column And Target.Row > 14 Then
  FileCopy "\\UKSH000-FILE06\purchasing\New_Supplier_Set_Ups_&_Audits\assets\audit.xls", "\\UKSH000-FILE06\purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\" & Range("B" & ActiveCell.Row) & "\audit.xls"
 End If

Simplifying your paths a bit for clarity:

Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FileExists("\\path\to\destination" & stuff & "\audit.xls") Then
    FileCopy "\\path\to\source\audit.xls", "\\path\to\destination\" & stuff & "\audit.xls"
End If

FileCopy(sourceString,DestinationString,Boolean)如果可以覆盖目标,则将Boolean设置为TRUE,默认情况下为false。

Really old thread but apparently the VBA FileCopy statement does not support the boolean parameter described in Kunal's answer ( https://docs.microsoft.com/it-it/office/vba/language/reference/user-interface-help/filecopy-statement ). On the other hand, the CopyFile method of the Scripting.FileSystemObject does ( https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/copyfile-method ).

FileSystemObject.CopyFile "c:\mydocuments\letters\file.doc", "c:\tempfolder\", True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM