Capturing user name

S

Syed Faisal

I am trying to keep a running log of every user who opens a workbook. On
startup, I'm running a macro that includes the "Application.UserName"
command to get the user name. However, many users of the workbook have
Excel set up so that the username returned by "Application.UserName" is
simply "Registered User". The "Registered User" seems to be an Excel
artifact only, based on what is entered in "Tools > Options > General > User
name"

Is there a way in VB to get a "lower-level" user name, from Windows itself
maybe? Because even on the machines that have "Registered User" in Excel,
the actual person's name can be found in Windows (2000) "My Computer"
properties. I would like to somehow bypass Excel's "Registered User" value
and get the person's real user name as set in Windows.

TIA.

-SF
 
P

Paul D

This came from a previous post by Chip Pearson. You can probably find more
info on his website
http://www.cpearson.com/excel.htm
Paul

Option Explicit

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

Public Declare Function GetComputerName Lib "kernel32" Alias
"GetComputerNameA" ( _
ByVal lpBuffer As String, nSize As Long) As Long

Sub AAA()
Dim CName As String
Dim UName As String
Dim CL As Long
Dim UL As Long
CName = String(255, " ")
UName = String(255, " ")
CL = 255
UL = 255
GetUserName UName, UL
GetComputerName CName, CL
UName = Left(UName, UL - 1) ' yes, -1. Thanks, MS, for consistency
CName = Left(CName, CL)
MsgBox "Username is: " & UName
MsgBox "Computer name is: " & CName
End Sub
 
L

ljb

Is the environment variable "username" set to the correct user? I'm not sure
if this depends on how the PC was setup in your organization. Might try

MsgBox Environ$("username")
 
E

Erin

Public UserName As String

Private Sub Workbook_Open()
WhoIsThis UserName
end sub

Sub WhoIsThis(UserName As String)
Dim UserLength As Long
UserName = String(255, " ")
UserLength = 255
GetUserName UserName, UserLength
UserName = Left(UserName, UserLength - 1)
End Sub

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

-----Original Message-----
I am trying to keep a running log of every user who opens a workbook. On
startup, I'm running a macro that includes the "Application.UserName"
command to get the user name. However, many users of the workbook have
Excel set up so that the username returned by "Application.UserName" is
simply "Registered User". The "Registered User" seems to be an Excel
artifact only, based on what is entered in "Tools > Options > General > User
name"

Is there a way in VB to get a "lower-level" user name, from Windows itself
maybe? Because even on the machines that
have "Registered User" in Excel,
the actual person's name can be found in Windows (2000) "My Computer"
properties. I would like to somehow bypass
Excel's "Registered User" value
 
B

Bob Phillips

Syed,

Here's a simple little function to get it

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

Public Function UserName() As String
Dim sName As String * 256
Dim cChars As Long
cChars = 256
If GetUserName(sName, cChars) Then
UserName = Left$(sName, cChars - 1)
End If
End Function



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

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

Similar Threads

userName 6
UserName Property 12
Register user on worbook open 2
User Name 4
environ username vs. application username 4
Getting the User Name 8
Shapes in 2010 1
locate add-in 5

Top