Excel VBA - 检查是否存在较新的.xlsm

时间:2016-01-24 11:02:43

标签: excel vba excel-vba

你可以帮我解决这个问题吗?我有.xlsm文件,它是通过公司分发的。每个公司成员都有一个应对并且可以访问网络驱动器上的相同文件夹(例如V:/ Excel / Apps)我发现了一个小函数来检查文件是否存在:

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

但我真的不知道如何检查同名文件是否更新。 我可以直接在文件名中使用版本,但它并没有解决我的问题,因为我的例如excelApp_0.1.xlsm无法知道,如果较新的搜索版本将是v0.2或0.8或1.6。你有什么建议吗?

2 个答案:

答案 0 :(得分:1)

使用obj_fso.getFile(filespec)获取FILE对象,然后使用file.DateCreated

答案 1 :(得分: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