get username

  • Thread starter Thread starter luke
  • Start date Start date
L

luke

how can I get excel (or what vb code is needed) to
display the current username that is using the file? or
to display a list of usernames on the domain?
 
This should display the current username

Private Declare Function w32_WNetGetUser Lib "mpr.dll" Alias _
"WNetGetUserA" (ByVal lpszLocalName As String, _
ByVal lpszUserName As String, lpcchBuffer As Long) As Long

Sub userTest()
Dim user$
Dim lngth&
Dim res&
user$ = String(256, Chr$(0))
res& = w32_WNetGetUser(vbNullString, user$, 256)
If res& = 0 Then
user$ = Left$(user$, InStr(1, user$, Chr$(0)) - 1)
Else
user$ = "Can't find username"
End If
MsgBox user$
End Sub

' I don't know a way to enumerate the users.
 
Here's another one. Most implementations I've seen use this API for getting
the network username.

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

Function GetCurrentUser() As String
Const MAX_USERNAME = 256
Dim strTemp As String * MAX_USERNAME

If GetUserName(strTemp, MAX_USERNAME) Then GetCurrentUser =
Left(strTemp, InStr(1, strTemp, Chr(0)) - 1)
End Function
 
Apologies, my brain's going. I thought I read "Network user" in the OP's
question, hence the w32_WNetGetUser stuff.

Frank is, of course right about the sensible way to get the computer user.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top