PDA

View Full Version : [SOLVED] Populating A cell with the users name



austenr
12-17-2004, 11:02 AM
Does anyone know if it is possible to populate a cell with the users name from a network?

austenr
12-17-2004, 11:43 AM
What I mean is can you pull the user name from the network and paste it into a cell?

Killian
12-17-2004, 11:52 AM
I've used
Application.UserName
on a domain based NT network to get the users login name

austenr
12-17-2004, 12:37 PM
Thanks.. I'll Try it

Ivan F Moala
12-17-2004, 07:29 PM
Application.Username gets the Applications default user name setup and not their loged on User name.

There are many ways to get this...here is one



Option Explicit

Private Declare Function Get_User_Name _
Lib "advapi32.dll" _
Alias "GetUserNameA" ( _
ByVal lpBuffer As String, _
nSize As Long) _
As Long

Private Function NTUserName() As String
'// Returns the NT login username
Dim lpBuff As String * 25
Get_User_Name lpBuff, 25
NTUserName = Left(lpBuff, Len(lpBuff) - 1)
End Function

Private Function NetworkUsername()
'// Returns the (Network) Username from the Environment variables
Dim str As String
Dim i As Integer
i = 1
'// looks for NWUSERNAME in environment variables
Do Until Left(str, 10) = "NWUSERNAME"
str = Environ$(i)
i = i + 1
If i > 255 Then Exit Do
Loop
If i < 255 Then
'// Just cuts off the first 11 characters ("NWUSERNAME=")
NetworkUsername = Right(str, Len(str) - 11)
Else
NetworkUsername = "No Network Name"
End If
End Function

Ken Puls
12-18-2004, 04:15 PM
Hi Austen,

Another example, which doesn't require the API is in our Knowlege Base here (http://www.vbaexpress.com/kb/getarticle.php?kb_id=234)

This function can be used to pull either the MS Office username, Windows Login name or the Computer Name.

HTH,

stapuff
01-17-2005, 12:30 PM
kpuls -

Thanks for the post. I incorporated this into a userform application.

Again - Thanks,

Kurt

Ken Puls
01-17-2005, 12:36 PM
Cheers, Kurt! Glad you found it useful!

Take care,