View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Paul D[_2_] Paul D[_2_] is offline
external usenet poster
 
Posts: 38
Default Capturing user name

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

"Syed Faisal" wrote in 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
and get the person's real user name as set in Windows.

TIA.

-SF