MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Returning User Logon ID in a Cell


Posted by John on January 30, 2002 12:07 PM

I would like help with a macro or function to display the user's logon id (Windows NT) in a cell. Can this be done easily?

Thanks.

John


Posted by Larry on January 30, 2002 12:30 PM

**** This will provide you with the user name of computer:

MsgBox Application.UserName

or

ActiveCell = Application.UserName

Posted by Juan Pablo G. on January 30, 2002 1:13 PM

Actually, it won't (All the time). Try this instead, returns the WinNT username.

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Dim ArchAct, BD As Workbook, BDI, BDE, BDR, BDS As Worksheet

Function NTUser() As String
On Error GoTo ErrHandler

Dim sDllFound As String

sDllFound = Dir("C:\WINNT\System32\advapi32.dll") & Dir("C:\WINNT1\System32\advapi32.dll")
If sDllFound <> vbNullString Then
Dim strBuffer As String * 255
Dim lngBufferLength As Long
Dim lngRet As Long
Dim strTemp As String

lngBufferLength = 255
lngRet = GetUserName(strBuffer, lngBufferLength)
strTemp = UCase(Trim(strBuffer))

NTUser = Left$(strTemp, InStr(strTemp, Chr$(0)) - 1)
Exit Function
End If

NTUser = vbNullString

ExitHandler:
Exit Function

ErrHandler:
'MsgBox (Err.Number & vbCrLf & Err.Description)
'Resume Next

End Function


To use it, in a cell put

=NTUser()

Juan Pablo G.

Posted by Aladin Akyurek on January 30, 2002 4:27 PM

OK, Juan: You asked for it.

Can you save (preferably encripted) the Excel file with as filename the username every 10 minutes and after it has been open for 45 minutes to a directory that the user/owner cannot find/access?

Can you disable the mail client temporarily on a PC?
All of the above in a Windows NT environment.

Aladin

========= Actually, it won't (All the time). Try this instead, returns the WinNT username. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long NTUser = vbNullString ExitHandler: ErrHandler: 'MsgBox (Err.Number & vbCrLf & Err.Description) 'Resume Next End Function

Posted by Juan Pablo G. on January 30, 2002 5:25 PM

> Can you save (preferably encripted) the Excel file with as filename the username every 10 minutes and after it has been open for 45 minutes to a directory that the user/owner cannot find/access?

Don't know about encription, would have to look, but the others, more doable.

> Can you disable the mail client temporarily on a PC?

Will look into this too.

Juan Pablo G. OK, Juan: You asked for it. Can you save (preferably encripted) the Excel file with as filename the username every 10 minutes and after it has been open for 45 minutes to a directory that the user/owner cannot find/access? Can you disable the mail client temporarily on a PC? All of the above in a Windows NT environment. Aladin

Posted by Aladin Akyurek on January 31, 2002 3:23 PM

Juan,

Encryption is the least important issue. I'll let you know thru mail why I'm interested in this at all.

Aladin

=========== > Can you save (preferably encripted) the Excel file with as filename the username every 10 minutes and after it has been open for 45 minutes to a directory that the user/owner cannot find/access? Don't know about encription, would have to look, but the others, more doable. > Can you disable the mail client temporarily on a PC? Will look into this too. Juan Pablo G. : OK, Juan: You asked for it. : Can you save (preferably encripted) the Excel file with as filename the username every 10 minutes and after it has been open for 45 minutes to a directory that the user/owner cannot find/access? : Can you disable the mail client temporarily on a PC? : All of the above in a Windows NT environment. : Aladin

Posted by Ivan F Moala on January 31, 2002 5:01 PM

Aladin

Is this for security ??


ivan Juan, Encryption is the least important issue. I'll let you know thru mail why I'm interested in this at all. Aladin =========== : > Can you save (preferably encripted) the Excel file with as filename the username every 10 minutes and after it has been open for 45 minutes to a directory that the user/owner cannot find/access? : Don't know about encription, would have to look, but the others, more doable. : > Can you disable the mail client temporarily on a PC? : Will look into this too. : Juan Pablo G.

Posted by Aladin Akyurek on February 01, 2002 4:46 AM

Hi Ivan,

> Is this for security ??

Yep, and to guarantee that the work has been really saved as <User Logon ID>.xls to a directory which can be read by nobody else but me.

Aladin