【发布时间】:2014-12-02 05:30:36
【问题描述】:
我知道有一些关于这个主题的类似帖子。但是,我的代码与我在这里看到的所有代码都不同(在谈论这个主题时)。
我收到的错误是找不到文件。但这有点不可能,因为我在 fso.CopyFile 中用作 SOURCE 的同一文件夹中搜索文件。
所以我必须修复这个错误,如果可能的话,我想将文件复制到另一个文件夹并更改名称。例如,如果我有文件“Excel.xls”,我想用名称“Excel_old.xls”进行复制,是否可以使用下面的代码,还是太难不值得?
这是代码:
Sub CopyFiles()
'Macro to copy all files modified yesterday
Dim n As String, msg As String, d As Date
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set fils = fso.GetFolder("C:\Users\Desktop\Files\").Files
'Verify all files in the folder, check the modification date and then copy
'to another folder (named Old)
For Each fil In fils
n = fil.Name
d = fil.DateLastModified
If d >= Date - 1 Then
file = n
'The following line is where the error occurs
fso.CopyFile "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file"
End If
Next fil
End Sub
【问题讨论】: