2014-10-07 84 views
1

我已经看到一些非常相似的问题,但是他们都没有找到我正在寻找的答案。其中一些没有回答。宏以验证上次修改文件的时间

我有一个VBA宏,通过搜索文件和验证文件创建时验证在某个日期(在这种情况下,昨天)中创建的所有文件。由于宏在互联网中被发现,我不确定这些对象是如何工作的。

我想知道是否有一种方法来改变fill.DateCreated类似的东西,但不是在文件创建时检查日期,宏将验证文件被修改时。起初它似乎很简单,但现在我真的很难得到这个。谁能帮我这个?

Sub VerifyNewFiles() 

Dim n As String, msg As String, d As Date 
msg = "" 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set fils = fso.GetFolder("C:\Users\Desktop\").Files 

For Each fil In fils 
    n = fil.Name 
    d = fil.DateCreated 

    If d >= Date - 1 Then 
     msg = msg & n & vbTab & d & vbCrLf 
    End If 
Next fil 

MsgBox msg 

Set fso = Nothing 

End Sub 

回答

2

f.DateLastModified可能会为你做。

如果您通过VBE中的工具 - >参考添加对Microsoft Scripting Runtime库的引用,您可以尽早绑定对象并获得智能感。

Sub VerifyNewFiles() 

    Dim fName As String, msg As String, fDate As Date 


    Dim fso As New FileSystemObject 
    Set fils = fso.GetFolder("C:\Users\" & Environ$("Username") & "\Desktop\").Files 

    Dim fil As File 
    For Each fil In fils 

     fName = fil.Name 
     fDate = fil.DateLastModified 

     If fDate >= Date - 1 Then msg = msg & fName & vbTab & fDate & vbCrLf 
    Next fil 

    MsgBox msg 

    Set fso = Nothing 
End Sub 

有库引用(Microsoft脚本运行时)添加到你的项目,你可以打开对象浏览器F2和选择库,探索它

enter image description here

+0

伟大,感谢您的提示! – dekio 2014-10-07 16:02:02

1

看起来你应该在每个循环的fil对象中使用DateLastModified属性。所以这个:

d = fil.DateCreated 

会变成这样:

d = fil.DateLastModified 

我建议你看看here有关DateLastModified属性的详细信息。