Créer un fichier log de l'utilisation d'un classeur

Je voudrais qu'à chaque ouverture d'un classeur particulier soit enregistré la date et l'heure d'ouverture mais également le nom de l'utilisateur.

Dans le module ThisWorkbook de ton classeur:

 Private Declare Function GetUserName Lib "advapi32.dll" _
 Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
 
 Private sub Workbook_BeforeClose(Cancel As Boolean)
 LogUserAction "Closed"
 end sub
Private sub Workbook_Open()
 LogUserAction "Opened"
 end sub
Function UserName()
 Dim S As String, n As Long, Res As Long
 S = String$(200, 0): n = 199: Res = GetUserName(S, n)
 UserName = UCase(Left(S, n - 1))
 End Function
sub LogUserAction(Action As String)
 Dim f As Integer, HistLog As String
 HistLog = Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1)
 HistLog = ThisWorkbook.Path & "\" & HistLog & ".txt"
 f = FreeFile
 Open HistLog For Append Shared As #f
 Write #f, Format(Now, "yyyy-mm-dd hh:mm:ss"), UserName, Action
 Close #f
 end sub

Auteur :

Mots clefs associés à cette page : , , , , , ,