簡體   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