Em Thisworkbook, coloque:
Private Sub Workbook_Open()
If ThisWorkbook.ReadOnly Then
DisplayCurrentUser
Else
LogCurrentUser ' log the last user information
' or add the last user information to the log history
LogThisUserAction "Opened"
End If
End Sub
Sub Workbook_BeforeClose (Cancel As Boolean)
If Not ThisWorkbook.ReadOnly Then
KillCurrentUserLog ' delete the last user information
End If
' or add the last user information to the log history
LogThisUserAction "Closed"
End Sub
Crie um módulo com o nome de mdl_LogUserHistory, cole o código abaixo:
' Author: Ole P. ErlandsenOption Explicit
' API declarationsDeclare Function GetComputerName Lib "kernel32" _Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As LongDeclare Function GetUserName Lib "advapi32.dll" _Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Sub LogCurrentUser()
' writes information to the logfile, you can call this from the Workbook_Open procedureDim f As Integer, LogFile As StringLogFile = ThisWorkbookLogFileNameIf Len(LogFile) = 0 Then Exit Sub ' workbook not saved yet...f = FreeFileOn Error Resume Next ' ignores any logging errorsOpen LogFile For Output As #fWrite #f, Format(Now, "yyyy-mm-dd hh:mm:ss"), Application.UserName, _ReturnUserName, ReturnComputerNameClose #fOn Error GoTo 0
End Sub
Sub KillCurrentUserLog()
' deletes the logfile, you can call this from the Workbook_BeforeClose procedureDim LogFile As StringLogFile = ThisWorkbookLogFileNameOn Error Resume NextKill LogFileOn Error GoTo 0
End Sub
Sub DisplayCurrentUser()
' displays the information in the log fileDim f As Integer, LogFile As StringDim strDateTime As String, strAppUserName As StringDim strUserID As String, strComputerName As StringLogFile = ThisWorkbookLogFileNameIf Len(LogFile) = 0 Then Exit Sub ' workbook not saved yet...f = FreeFileOn Error Resume Next ' ignores any logging errorsOpen LogFile For Input Access Read Shared As #fInput #f, strDateTime, strAppUserName, strUserID, strComputerNameClose #fOn Error GoTo 0
MsgBox "User name: " & strAppUserName & Chr(13) & _"UserID: " & strUserID & Chr(13) & _"Computer name: " & strComputerName & Chr(13) & _"Date/time: " & strDateTime & Chr(13), _vbInformation, ThisWorkbook.Name & " is in use by:"
End SubFunction ThisWorkbookLogFileName() As String
' returns the filename used for logging informationThisWorkbookLogFileName = ""If Len(ThisWorkbook.Path) = 0 Then Exit FunctionThisWorkbookLogFileName = Left(ThisWorkbook.FullName, Len(ThisWorkbook.FullName) - 3) & "log"
End FunctionFunction ReturnComputerName() As String
' returns the computer nameDim rString As String * 255, sLen As Long, tString As StringtString = ""On Error Resume NextsLen = GetComputerName(rString, 255)sLen = InStr(1, rString, Chr(0))If sLen > 0 ThentString = Left(rString, sLen - 1)ElsetString = rStringEnd IfOn Error GoTo 0ReturnComputerName = UCase(Trim(tString))
End FunctionFunction ReturnUserName() As String
' returns the Domain User NameDim rString As String * 255, sLen As Long, tString As StringtString = ""On Error Resume NextsLen = GetUserName(rString, 255)sLen = InStr(1, rString, Chr(0))If sLen > 0 ThentString = Left(rString, sLen - 1)ElsetString = rStringEnd If
On Error GoTo 0ReturnUserName = UCase(Trim(tString))
End Function
Crie outro módulo com o nome de mdl_LogCurrentUser, cole o código abaixo:
' Author: Ole P. ErlandsenOption Explicit' API declarationsDeclare Function GetComputerName Lib "kernel32" _Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As LongDeclare Function GetUserName Lib "advapi32.dll" _Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As LongSub LogThisUserAction(Action As String)
' writes information to the logfile, you can call this from the Workbook_Open procedureDim f As Integer, LogFile As StringLogFile = ThisWorkbookLogFileNameIf Len(LogFile) = 0 Then Exit Sub ' workbook not saved yet...f = FreeFileOn Error Resume Next ' ignores any logging errorsOpen LogFile For Append As #fWrite #f, Format(Now, "yyyy-mm-dd hh:mm:ss"), Application.UserName, _ReturnUserName, ReturnComputerName, ActionClose #fOn Error GoTo 0
End Sub
Sub DisplayLastUserAction()
' displays the last line of information in the log fileDim f As Integer, LogFile As StringDim strDateTime As String, strAppUserName As StringDim strUserID As String, strComputerName As StringLogFile = ThisWorkbookLogFileNameIf Len(LogFile) = 0 Then Exit Sub ' workbook not saved yet...f = FreeFileOn Error Resume Next ' ignores any logging errorsOpen LogFile For Input Access Read Shared As #fDo While Not EOF(f)Input #f, strDateTime, strAppUserName, strUserID, strComputerNameLoop ' until the last line is readClose #fOn Error GoTo 0
MsgBox "User name: " & strAppUserName & Chr(13) & _"UserID: " & strUserID & Chr(13) & _"Computer name: " & strComputerName & Chr(13) & _"Date/time: " & strDateTime & Chr(13), _vbInformation, "Last User Action:"
End SubFunction ThisWorkbookLogFileName () As String
' returns the filename used for logging informationThisWorkbookLogFileName = ""If Len(ThisWorkbook.Path) = 0 Then Exit FunctionThisWorkbookLogFileName = Left(ThisWorkbook.FullName, Len(ThisWorkbook.FullName) - 3) & "txt"
End FunctionFunction ReturnComputerName() As String
' returns the computer nameDim rString As String * 255, sLen As Long, tString As StringtString = ""On Error Resume NextsLen = GetComputerName(rString, 255)sLen = InStr(1, rString, Chr(0))If sLen > 0 ThentString = Left(rString, sLen - 1)ElsetString = rStringEnd IfOn Error GoTo 0ReturnComputerName = UCase(Trim(tString))
End FunctionFunction ReturnUserName() As String
' returns the Domain User NameDim rString As String * 255, sLen As Long, tString As StringtString = ""On Error Resume NextsLen = GetUserName(rString, 255)sLen = InStr(1, rString, Chr(0))If sLen > 0 ThentString = Left(rString, sLen - 1)ElsetString = rStringEnd IfOn Error GoTo 0ReturnUserName = UCase(Trim(tString))
End FunctionFunction AC (Row As Boolean) As Long
' returns the row- or columnnumber for the active cellLet AC = 0On Error Resume NextIf Row ThenLet AC = ActiveCell.RowElseLet AC = ActiveCell.ColumnEnd If
End Function
Tags: Excel, udf, doc, documentation, log, security, API, dll
André Luiz Bernardes