查找封闭工作簿的上次保存时间

我试图做一个macros,可以参考时,另一个工作簿上次更新,而无需打开它。

我发现这一个,但它只适用于活动工作簿:

Function LastModified() as Date LastModified = ActiveWorkbook.BuiltinDocumentProperties("Last Save Time") End Function 

我想要的是这样的:

 Function LastModified() as Date LastModified = path."c:/test.xlsx".BuiltinDocumentProperties("Last Save Time") End Function 

但是这当然不起作用。 我该如何解决?

像这样的事情应该做的伎俩。 在这个例子中你需要使用FileSystemObject 。 我在示例中使用了晚期绑定,但如果切换到早期绑定,则可能会获得一些性能提升。

 Option Explicit Private Function LastModified(ByRef filePath As String) As Date 'Set a default value LastModified = vbNull If Len(Trim$(filePath)) = 0 Then Exit Function With CreateObject("Scripting.FileSystemObject") If .FileExists(filePath) Then LastModified = .GetFile(filePath).DateLastModified End With End Function Sub SOExample() 'Specify a path to a file you want to get LastModified Date Dim filePath As String: filePath = "MyPathToAFile" 'Return a date or vbNull Dim myDate As Date: myDate = LastModified(filePath) 'Output the Date if it isn't vbNull If Not myDate = vbNull Then Debug.Print myDate End Sub 

Filesystemobject工作,但我需要然后将其打印到单元格,因为它不会调用该函数。

FileDateTime虽然为我工作:)

 Function LastModified() as Date LastModified = FileDateTime("filepath") End Function