2014-12-04 185 views
0

什麼是錯在這裏:的CopyFile Excel VBA中的路徑錯誤

defenition:

  Dim fso As Object, f As Object, ff As Object, f1 As Object 
      Dim new_des As Object 
      Application.ScreenUpdating = False 
      Set fso = CreateObject("Scripting.FileSystemObject") 
      Set f = fso.Getfolder("C:\test\new") 
      Set ff = f.Files 

      For Each f1 In ff...... 

      ....Set new_des = fso.Getfolder("C:\test\new\old" & "\" & f1)..... 

      ......FileCopy f1, new_des.... 

我已經運行時錯誤75路徑沒有找到 '設置new_des .....' 路徑是存在的 「F1」是抱着文件

回答

0

ffFiles集合,在每個f1是一個文件,以便

fso.Getfolder("C:\test\new\old" & "\" & f1) 

試圖在文件路徑上嘗試GetFolder,該文件路徑無效,因爲它需要目錄路徑。

您需要將完整路徑字符串路徑爲FileCopy

0

我聲明new_des作爲字符串和: new_des =( 「C:\測試\新\舊」) 然後我沒有: FileCopy(F & F1),(new_des & F1)

0

複製所有文件使用:

fso.CopyFolder "C:\test\new", "C:\test\new\old" 'from new to old 

,或者如果你想在你的代碼做,使用BuildPath()文件夾與文件串聯,GetFileName()拿到f1文件的名稱和定義new_des as String保存生成的PA th:

dim new_des as String 
new_des = fso.BuildPath("C:\test\new\old\", fso.GetFileName(f1))