'Public declarations. These 2 lines must be before all procedures
Public Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Dim X As New EventClassModule
Sub InitializeApp()
Set X.App = Application
End Sub
Sub Auto_Open()
Call InitializeApp
End Sub
Function ReturnUserName() As String
'Courtesy of http://www.exceltip.com/show_tip/Cu...er_name_using_VBA_in_Microsoft_Excel/452.html
' returns the NT Domain User Name
Dim rString As String * 255, sLen As Long, tString As String
tString = ""
On Error Resume Next
sLen = GetUserName(rString, 255)
sLen = InStr(1, rString, Chr(0))
If sLen > 0 Then
tString = Left(rString, sLen - 1)
Else
tString = rString
End If
On Error GoTo 0
ReturnUserName = UCase(Trim(tString))
End Function
Sub LogUser()
Dim FileNum As Integer, FilePath As String
'Set path to log file
FilePath = "c:\LogTest.txt" 'change this to a network folder everyone has access to
'Obtain next free IO number
FileNum = FreeFile()
'Open file
Open FilePath For Append As #FileNum
'Write user login and todays date and time to textfile
Write #FileNum, ReturnUserName, Now()
' Close file.
Close #FileNum
End Sub