VBA: how do I get full user name, not login ID?

mthompso

Board Regular
Joined
Apr 18, 2008
Messages
123
The following VBA code, executed on a Windows Network computer, will give me the Windows Login ID of the user that's logged in to the computer:
Code:
Sub UserName()
    MsgBox Environ("username")
End Sub
How do I get the full user name, not just the Windows Login ID?

Example: On this specific Windows network, the login id is the first letter of the user's first name followed by their last name. John Doe's login id is JDOE. How can I get VBA to give me the string "John Doe" and not "JDOE"?
 

Some videos you may like

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

DonkeyOte

MrExcel MVP
Joined
Sep 6, 2002
Messages
9,123
hmm... I always forget how global Mr E is and how absurd we are in the UK at times...

"To Faff": http://en.wiktionary.org/wiki/faff

Though I meant in the context that's it's a bit of b*ll ache / hassle for what you get out of it at the end ;)

It would be nice if we could pull this info easily as we can the NT username...
 

Haluk

Rules Violation
Joined
Oct 26, 2002
Messages
1,075
Try this;

Code:
Sub GetUserFullName()
    Dim MyOBJ As Object
    On Error Resume Next
    Set MyOBJ = GetObject("WinMgmts:").instancesOf("Win32_NetworkLoginProfile")
    If Err.Number <> 0 Then
      MsgBox "WMI has not been installed, code will be terminated...", vbExclamation, "Windows Management Instrumentation"
      Exit Sub
    End If
    For Each objItem In MyOBJ
        MyMsg = MyMsg & "User Full Name: " & vbCrLf & vbCrLf & objItem.FullName
    Next
    MsgBox MyMsg, vbInformation, "User Full Name ....   (Haluk ®)"
End Sub
 

Stormseed

Banned
Joined
Sep 18, 2006
Messages
3,274
hmm... I always forget how global Mr E is and how absurd we are in the UK at times...

"To Faff": http://en.wiktionary.org/wiki/faff
I can't open that website from here (office). Will check it from home !

Heck, it is great to learn new words, especially when it comes from the UK part of the world - because it is the origin of English. I forgot to mention, other than Excel information, MrExcel is main source of my learning of new english words almost everyday and I really like it though ;)

Just yesterday, I came across one more word called "duff" which was used by Patience & her friends on Facebook. I am yet to learn the meaning of that word too :LOL:

Thanks, Luke for the information you provided !
 

Haluk

Rules Violation
Joined
Oct 26, 2002
Messages
1,075
Another alternative is using the WSH approach;

Code:
Sub GetUserFullName2()
    Set WSHnet = CreateObject("WScript.Network")
    UserName = WSHnet.UserName
    UserDomain = WSHnet.UserDomain
    Set objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
    UserFullName = objUser.FullName
    MsgBox "User Full Name: " & vbCrLf & vbCrLf & UserFullName, vbInformation, "User Full Name ....   (Haluk ®)"
End Sub
 

Jhecyka BR

New Member
Joined
May 15, 2017
Messages
1
Another alternative is using the WSH approach;

Code:
Sub GetUserFullName2()
    Set WSHnet = CreateObject("WScript.Network")
    UserName = WSHnet.UserName
    UserDomain = WSHnet.UserDomain
    Set objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
    UserFullName = objUser.FullName
    MsgBox "User Full Name: " & vbCrLf & vbCrLf & UserFullName, vbInformation, "User Full Name ....   (Haluk ®)"
End Sub

Thank you very much! I was looking for a long time how to pull the username windows, and finally found! <3 <3 <3
 

Dokat

Board Regular
Joined
Jan 19, 2015
Messages
122
Hi - Code works perfectly when i am logged on to the network but it doest work when i am offsite and working remotely. Is there a way to modify the code so that it can work when i am not on network
 

Watch MrExcel Video

Forum statistics

Threads
1,099,699
Messages
5,470,250
Members
406,686
Latest member
BNR_ 1980

This Week's Hot Topics

Top