![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Mar 2002
Posts: 34
|
Is there a way I can get the name of the user logged onto Windows to use in Excel VBA to name files, put the username into macros and formulas, etc. I have many uses for this kind of functionality in Excel and (if not too off topic) Access as well. I'm sure I've seen this elsewhere before, but can't remember where or how. We use Office 2000 on Win98 and 2000 workstations.
Thanks |
|
|
|
|
|
#2 |
|
Guest
Posts: n/a
|
Application.UserName
|
|
|
|
#3 |
|
Board Regular
Join Date: Feb 2002
Location: Guderup, Denmark
Posts: 287
|
Try this function GetUserName()
Option Explicit ' This is used by GetUserName() to find the current user's ' name from the API Declare Function Get_User_Name Lib "advapi32.dll" Alias _ "GetUserNameA" (ByVal lpBuffer As String, _ nSize As Long) As Long Function GetUserName() As String Dim lpBuff As String * 25 Get_User_Name lpBuff, 25 GetUserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1) End Function |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
The following code returns the username of excel not windows:
Application.UserName I hope this helps. Kind regards, Al |
|
|
|
|
|
#5 |
|
Guest
Posts: n/a
|
Application.UserName
|
|
|
|
#6 |
|
Board Regular
Join Date: Feb 2002
Location: Houston, TX
Posts: 305
|
On Excel running over a network, I had occasion to use this where UserName returned useless info like the company name.
Sh.[A1] = UserLoggedIn |
|
|
|
|
|
#7 | |
|
Board Regular
Join Date: Jul 2002
Location: Charlotte, NC
Posts: 417
|
Quote:
If we have a button on a worksheet that presents a msgbox with the username where do I put the above code? Thank you, Michael |
|
|
|
|
|
|
#8 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Helena, MT
Posts: 13,690
|
The code to return the log-on name is
Code:
Environ("UserName")
Code:
Application.UserName lenze
__________________
If you have to tell your boss you're good with Excel, you're NOT!! All I know about Excel I owe to my ignorance! Scotch: Because you don't solve great Excel problems over white wine |
|
|
|
|
|
#9 |
|
Board Regular
Join Date: Jul 2002
Location: Charlotte, NC
Posts: 417
|
I like simple. Thank you.
|
|
|
|
|
|
#10 |
|
Board Regular
Join Date: Aug 2006
Location: Rose of the Shires
Posts: 215
|
I found this solution worked well, as Environ("Username") just returned the Users login initials, and not the Users actual name.
Code:
Option Explicit
Sub Users_Fullname()
'originally coded as VB script by A.Vials, converted to VBA by Sly
Dim objInfo
Dim strLDAP
Dim strFullName
Set objInfo = CreateObject("ADSystemInfo")
strLDAP = objInfo.UserName
Set objInfo = Nothing
strFullName = GetUserName(strLDAP)
MsgBox "Full name of User is " & strFullName 'step to test
End Sub
Function GetUserName(strLDAP)
Dim objUser
Dim strName
Dim arrLDAP
Dim intIdx
On Error Resume Next
strName = ""
Set objUser = GetObject("LDAP://" & strLDAP)
If Err.Number = 0 Then
strName = objUser.Get("givenName") & Chr(32) & objUser.Get("sn")
End If
If Err.Number <> 0 Then
arrLDAP = Split(strLDAP, ",")
For intIdx = 0 To UBound(arrLDAP)
If UCase(Left(arrLDAP(intIdx), 3)) = "CN=" Then
strName = Trim(Mid(arrLDAP(intIdx), 4))
End If
Next
End If
Set objUser = Nothing
GetUserName = strName
End Function
__________________
GriZlore Statistics are like a lamp-post to a drunken man - more for leaning on than illumination. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|