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"?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
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...
 
Upvote 0
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
 
Upvote 0
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 !
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top