Get User Name

aziz rasul

Active member
Local time
Today, 05:08
Joined
Jun 26, 2000
Messages
1,935
I have a MS Excel 2010 file that is not shared. I'm looking for code that will give me the user name of the person who has the file currently open.

Thanks in advance.
 
Thanks Ken, I will try that tomorrow and come back to if I have any problems.
 
A solution provided to me by another user (I can't find who/where now):

Code:
Option Compare Database
Option Explicit

'Get user name
 Declare Function WNetGetUser Lib "mpr.dll" _
  Alias "WNetGetUserA" (ByVal lpName As String, _
  ByVal lpUserName As String, lpnLength As Long) As Long
  
Const NoError = 0           'The Function call was successful
   
Function GetUserName() As String

   Dim LUserName As String
   Const lpnLength As Integer = 255
   Dim status As Integer
   Dim lpName

   ' Assign the buffer size constant to lpUserName.
   LUserName = Space$(lpnLength + 1)
   
   ' Get the log-on name of the person using product.
   status = WNetGetUser(lpName, LUserName, lpnLength)
   
   ' See whether error occurred.
   If status = NoError Then
      ' This line removes the null character. Strings in C are null-
      ' terminated. Strings in Visual Basic are not null-terminated.
      ' The null character must be removed from the C strings to be used
      ' cleanly in Visual Basic.
      LUserName = Left$(LUserName, InStr(LUserName, Chr(0)) - 1)
      
   Else
      ' An error occurred.
      MsgBox "Unable to get the name."
      End
   End If
   
   GetUserName = LUserName
   
End Function

Hope it helps you too

Edit: Oops, missed a bit! code complete now
 
Last edited:

Users who are viewing this thread

Back
Top Bottom