2

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

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

3

Really old thread but apparently the VBA FileCopy statement does not support the boolean parameter described in Kunal's answer (https://learn.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://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/copyfile-method).

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

Comments

1

FileCopy (sourceString, DestinationString, Boolean) Set Boolean to TRUE if destination can be overwritten, by default it is false.

1 Comment

FileSystemObject has a Boolean to overwrite, but FileCopy does not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.