繁体   English   中英

excel vba - 将文件从一个文件夹复制到另一个文件夹,但如果文件存在不覆盖?

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

有人可以告诉我一种使用 vba 将文件从一个文件夹复制到另一个文件夹的方法,但有条件说明文件是否已经存在不要覆盖吗?

这是我的代码:

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

为简化起见,简化了路径:

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。

非常旧的线程,但显然 VBA FileCopy 语句不支持 Kunal 的回答中描述的布尔参数( https://docs.microsoft.com/it-it/office/vba/language/reference/user-interface-help/filecopy-声明)。 另一方面, Scripting.FileSystemObject 的 CopyFile 方法( 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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