使用动态名称获取目录中最后修改的文件或文件夹

时间:2018-09-26 15:17:12

标签: vba excel-vba

我试图在excel表中显示最后一个访问excel文件和时间的人,但是文件名每天都在变化(文件名包含日期)。

我当前的代码给我一个找不到文件的错误,我尝试了ActiveWorkbook.pathApplication.ActiveWorkbook.Path

Sub Main()
'Setting Summary Report
Dim sAuthor As String
sAuthor = ActiveWorkbook.BuiltinDocumentProperties("Last Author")
 'Last modified person & time
    Dim fileModDate As String
    Dim fs
    Dim f
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(Application.ActiveWorkbook.Path)
    fileModDate = f.DateLastModified
    Worksheets("Sheet1").Range("A2") = sAuthor & " " & fileModDate
End Sub

我可以将文件重新保存为测试或类似名称的统一名称,但是我不确定是否可以修改代码以使其正常工作,因此我不必重新保存文件。

1 个答案:

答案 0 :(得分:1)

猜猜我会发布一个答案来结束这个问题

Sub Main()
    'Setting Summary Report

    'Last author
    Dim sAuthor As String
    sAuthor = ActiveWorkbook.BuiltinDocumentProperties("Last Author")

    'Last modified time
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim file As Object
    Set file = fso.GetFile(ActiveWorkbook.FullName)

    Dim fileModDate As String
    fileModDate = file.DateLastModified

    'Updating range to last author and modified time
    Worksheets("Sheet1").Range("A2").Value2 = sAuthor & " " & fileModDate

End Sub