The opening/closing stuff sounds awfully complicated. It would be easier for the user to open the workbooks that they want using the normal Excel methods. (Typing file paths is extremely prone to typos)
The logging of information can be automated. Just log every time that a workbook is opened, saved, save As'ed or NEW.
The attached does that via a class module.
Put this code in a class module named clsWBLogger
' in class module clsWBLogger
Public WithEvents MyWorkbook As Workbook
Public WithEvents ThisApp As Application
Dim SaveFlag As Boolean
Dim SaveTime As Date
Dim SaveBeforeName As String
Dim CloseFlag As Boolean
Dim CloseBookName As String
Dim CloseBookFullName As String
Private Sub Class_Initialize()
LogAction Now, "Logger On"
Set MyWorkbook = ThisWorkbook
Set Me.ThisApp = ThisWorkbook.Application
End Sub
Private Sub Class_Terminate()
Set ThisApp = Nothing
Set MyWorkbook = Nothing
LogAction Now, "Logger off"
ThisWorkbook.Save
End Sub
Private Sub MyWorkbook_BeforeClose(Cancel As Boolean)
LogAction Now, "Logger off"
ThisWorkbook.Save
Set ThisApp = Nothing
Set MyWorkbook = Nothing
End Sub
Private Sub ThisApp_NewWorkbook(ByVal Wb As Workbook)
LogAction Now, Wb.Name, Wb.FullName, "New"
End Sub
Private Sub ThisApp_WorkbookActivate(ByVal Wb As Workbook)
Dim StillOpen As Boolean
If CloseFlag Then
On Error Resume Next
StillOpen = (Workbooks(CloseBookName).Name = CloseBookName)
On Error GoTo 0
If Not StillOpen Then
LogAction Now, CloseBookName, CloseBookFullName, "closed"
End If
End If
CloseFlag = False
CloseBookName = vbNullString
CloseBookFullName = vbNullString
End Sub
Private Sub ThisApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
CloseFlag = True
CloseBookName = Wb.Name
CloseBookFullName = Wb.FullName
End Sub
Private Sub ThisApp_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
SaveFlag = True
SaveTime = Now
SaveBeforeName = Wb.FullName
Else
If Wb.FullName <> ThisWorkbook.FullName Then
LogAction Now, Wb.Name, Wb.FullName, "saved"
End If
End If
End Sub
Private Sub ThisApp_WorkbookDeactivate(ByVal Wb As Workbook)
If SaveFlag Then
LogAction SaveTime, Wb.Name, Wb.FullName, "saved As", "was: " & SaveBeforeName
If CloseFlag Then
CloseBookName = Wb.Name
CloseBookFullName = Wb.FullName
End If
End If
SaveFlag = False
SaveBeforeName = vbNullString
End Sub
Private Sub ThisApp_WorkbookOpen(ByVal Wb As Workbook)
If ThisWorkbook.FullName <> Wb.FullName Then
LogAction Now, Wb.Name, Wb.FullName, "open"
End If
End Sub
Sub LogAction(TimeStamp As Date, ParamArray Detail() As Variant)
Dim i As Long
With NextRow
.Cells(1, 1).Value = TimeStamp
If 0 <= UBound(Detail) Then
For i = 0 To UBound(Detail)
.Cells(1, 2).Offset(0, i).Value = Detail(i)
Next i
End If
End With
End Sub
Function NextRow() As Range
Set NextRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
End Function
And this code in the ThisWorkbook code module
' in ThisWorkbook code module
Public Logger As Variant
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set Logger = Nothing
End Sub
Private Sub Workbook_Open()
Set Logger = New clsWBLoger
End Sub
That will automatically log all Opening, Closeing, Saving and Newing actions of all the workbooks.
If you want to stop or re-start the logger, this code in a normal module will do that.
' in normal module
Sub StopLogger()
Set ThisWorkbook.Logger = Nothing
End Sub
Sub ReStartLogger()
Set ThisWorkbook.Logger = Nothing
Set ThisWorkbook.Logger = New clsWBLoger
End Sub
If you want further filtering or sorting of the data collected, an additional sheet should be added and other excel methods used to extract the data that you are interested in.
Bookmarks