2016-01-24 71 views
0

你能幫我解決這個問題嗎?我有分佈在公司的.xlsm文件。每個公司成員有一個適應和訪問同一個文件夾的網絡驅動器上(例如五:/ EXCEL /應用),我發現了一個小功能,以檢查文件是否存在:Excel VBA - 檢查是否存在新的.xlsm

Function fileExists(s_directory As String, s_fileName As String) As Boolean 

    Dim obj_fso As Object 

    Set obj_fso = CreateObject("Scripting.FileSystemObject") 
    fileExists = obj_fso.fileExists(s_directory & "\" & s_fileName) 

End Function 

但I'really不要」不知道如何檢查具有相同名稱的文件是否更新。 我可以直接在文件名中使用版本,但它不能解決我的問題,因爲我的例如excelApp_0.1.xlsm無法知道,如果較新的搜索版本將是v0.2或0.8或1.6。你有什麼建議嗎?

回答

1

使用obj_fso.getFile(filespec)獲取FILE對象,然後使用file.DateCreated

0

感謝UncleO,它的工作原理:-)

Function newerFileExists(s_directory As String, s_fileName As String) As Boolean 
    Dim obj_fso As Object 

    Set obj_fso = CreateObject("Scripting.FileSystemObject") 
    Dim fileExists As Boolean 
    fileExists = obj_fso.fileExists(s_directory & "\" & s_fileName) 
    If fileExists Then 
     Dim file As Object 
     Dim s As String 
     Dim currentDate As Object 
     Dim newerDate As String 
     Set file = obj_fso.getFile(s_directory & "\" & s_fileName) 
     Set currentDate = ActiveWorkbook.BuiltinDocumentProperties("Creation Date") 
     newerDate = file.DateCreated 
     's = "Current file creation date: " & currentDate & vbCrLf 
     's = s & "Newer file creation date: " & newerDate 
     'MsgBox s 
     If newerDate > currentDate Then 
      newerFileExists = True 
     Else 
      newerFileExists = False 
     End If 
    Else 
    newerFileExists = False 
    End If 
End Function