2017-05-23 75 views
0

我想用修改後的名稱在不同位置創建excel文件的acopy。下面的代碼使用修改後的名稱移動文件而不是複製。VBA創建移動而不是移動文件

我不想從原始位置刪除文件只是想創建一個副本。請協助。與您可以asynchroneously恢復代碼的優勢

CreateObject("WScript.Shell").Run("cmd.exe /c xcopy /y /s """ & Source & """ """ & Dest & """", WindowStyle:=7 , WaitOnReturn:=(your boolean)) 

Dim myFileNameDir As String 
Dim ws1 As Worksheet 
myFileNameDir = Sheet1.Range("V9").Value & SPID1 & "\" & ComboBox29.Text 
scor = ComboBox29.Text 
scor = Replace(scor, ".", "") 
MsgBox myFileNameDir 

filenz = SPID1 & "_" & Emp1 & "_" & scor & "_" & VBA.Format(Now, "MMddyyyyhmmss AM/PM ") 
Dim myfile As String 
myfile = Sheet1.Range("V10").Value & filenz & ".xlsx" 
Dim fso As Object 
Set fso = VBA.CreateObject("Scripting.FileSystemObject") 
Call fso.CopyFile(myFileNameDir, myfile) 
+1

是什麼讓你覺得這是移動文件,而不是複製?我只要求'FileSystemObject.CopyFile' **爲**複製**文件,**不是**移動它們。也許你的程序中有進一步的代碼會刪除原始代碼? – CLR

回答

0

我從來沒有嘗試過的FSO副本,但這應該做的。

0

這複製了文件,同時保留原始文件。它基本上和你的代碼一樣,所以@CLR說 - 也許你的程序中有更多的代碼會刪除原來的代碼?

Sub Test() 

    MsgBox CopyFile("C:\_Test\A\New Microsoft Excel Worksheet.xlsx", _ 
        "C:\_Test\B\Copy Of File.xlsx", False) 

End Sub 

Function CopyFile(FromFile As String, ToFile As String, Overwrite As Boolean) As Boolean 

    Dim oFSO As Object 
    Set oFSO = CreateObject("Scripting.FileSystemObject") 

    On Error Resume Next 
    oFSO.CopyFile FromFile, ToFile, Overwrite 
    CopyFile = (Err.Number = 0) 
    Err.Clear 

End Function