【问题标题】:excel vba - copy file from one folder to another but if file exists do not overwrite?excel vba - 将文件从一个文件夹复制到另一个文件夹,但如果文件存在不覆盖?
【发布时间】:2014-09-25 08:15:53
【问题描述】:

谁能告诉我一种使用 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

【问题讨论】:

    标签: excel vba copy


    【解决方案1】:

    为了清晰起见,稍微简化一下路径:

    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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2014-12-02
          • 2016-02-19
          • 1970-01-01
          • 2022-08-04
          • 1970-01-01
          • 2011-08-22
          • 1970-01-01
          • 2023-01-14
          • 2020-03-20
          相关资源
          最近更新 更多