Imports System Imports System.IO Imports System.Diagnostics Imports System.Collections.Generic Imports Autodesk Imports Autodesk.Revit Imports Autodesk.Revit.DB Imports Autodesk.Revit.DB.Events Imports Autodesk.Revit.UI Imports Autodesk.Revit.UI.Events _ _ Partial Public Class ThisApplication Private m_dicLastSaved As New Dictionary(Of Document, DateTime)() Private Sub Module_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup InitializeDicLastSaved() AddHandler Me.Application.DocumentSaved, AddressOf ThisApplication_DocumentSaved AddHandler Me.Application.DocumentOpened, AddressOf ThisApplication_DocumentOpened AddHandler Me.Application.DocumentCreated, AddressOf ThisApplication_DocumentCreated AddHandler Me.Application.DocumentClosing, AddressOf ThisApplication_DocumentClosing End Sub Private Sub Module_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown RemoveHandler Me.Application.DocumentSaved, AddressOf ThisApplication_DocumentSaved RemoveHandler Me.Application.DocumentOpened, AddressOf ThisApplication_DocumentOpened RemoveHandler Me.Application.DocumentCreated, AddressOf ThisApplication_DocumentCreated RemoveHandler Me.Application.DocumentClosing, AddressOf ThisApplication_DocumentClosing End Sub ''' ''' Initialize ''' Private Sub InitializeDicLastSaved() For Each document As Document In Me.Application.Documents m_dicLastSaved.Add(document, DateTime.MaxValue) Next End Sub ''' ''' DocumentCreated event, add the created document into m_dicLastSaved ''' ''' sender ''' DocumentCreatedEventArgs Private Sub ThisApplication_DocumentCreated(ByVal sender As Object, ByVal args As Autodesk.Revit.DB.Events.DocumentCreatedEventArgs) m_dicLastSaved.Add(args.Document, DateTime.MaxValue) End Sub ''' ''' DocumentClosing event, remove the closing document from m_dicLastSaved ''' ''' sender ''' DocumentClosingEventArgs Private Sub ThisApplication_DocumentClosing(ByVal sender As Object, ByVal args As Autodesk.Revit.DB.Events.DocumentClosingEventArgs) m_dicLastSaved.Remove(args.Document) End Sub ''' ''' DocumentOpened event, add the opened document into m_dicLastSaved ''' ''' sender ''' DocumentOpenedEventArgs Private Sub ThisApplication_DocumentOpened(ByVal sender As Object, ByVal args As Autodesk.Revit.DB.Events.DocumentOpenedEventArgs) m_dicLastSaved.Add(args.Document, DateTime.MaxValue) End Sub ''' ''' DocumentSaved event, record the current DataTime for the saved document ''' ''' sender ''' DocumentSavedEventArgs Private Sub ThisApplication_DocumentSaved(ByVal sender As Object, ByVal args As Autodesk.Revit.DB.Events.DocumentSavedEventArgs) Me.m_dicLastSaved(args.Document) = DateTime.Now End Sub ''' ''' FormatTimeSpan ''' ''' TimeSpan ''' the string of TimeSpan Private Function FormatTimeSpan(ByVal elapse As TimeSpan) As [String] Dim elapseStr As [String] = elapse.ToString() Dim lastIndexOfDot As Integer = elapseStr.LastIndexOf("."c) Return elapseStr.Substring(0, lastIndexOfDot) End Function ''' ''' GetTimeElapsedSinceLastSave ''' Public Sub GetTimeElapsedSinceLastSave() Dim text As [String] = [String].Format("{0,-30}{1,-30}", "Document Full Name", "Elapse Time Since Last Save(day.hh:mm:ss)") & vbLf For Each pair As KeyValuePair(Of Document, DateTime) In m_dicLastSaved Dim strElapsed As [String] = [String].Empty If pair.Value = DateTime.MaxValue Then strElapsed = "Never" Else strElapsed = FormatTimeSpan(DateTime.Now - pair.Value) End If Dim fileName As [String] = System.IO.Path.GetFileName(pair.Key.PathName) If [String].IsNullOrEmpty(fileName) Then fileName = "*New*" End If text += [String].Format("{0,-30}{1,-30}", fileName, strElapsed) & vbLf Next System.Windows.Forms.MessageBox.Show(text, "Macro GetTimeElapsedSinceLastSave") End Sub End Class