ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loged On User Name (https://www.excelbanter.com/excel-programming/377527-loged-user-name.html)

jutlaux

Loged On User Name
 
Is there a way to get the the user name of the current user who is logged on
to a PC so that it can be inputted into a cell?

Jim Thomlinson

Loged On User Name
 
Assuming you want the windows login name then you can use a UDF something
like this (placed in a standard code module)

Public Function UserName() as String
UserName = Environ("UserName")
End Function

Now in the cell add the formula...
=UserName()

--
HTH...

Jim Thomlinson


"jutlaux" wrote:

Is there a way to get the the user name of the current user who is logged on
to a PC so that it can be inputted into a cell?


Gary''s Student

Loged On User Name
 
From Bob Phillips:

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



--
Gary's Student


"jutlaux" wrote:

Is there a way to get the the user name of the current user who is logged on
to a PC so that it can be inputted into a cell?


Jim Thomlinson

Loged On User Name
 
Environ("UserName") is a whole pile easier and based on a long drawn out
discussion on the topic (posted some time back) there is really no great
advantage to using the API that you posted... That being said the code you
posted works just fine (I used to use it myself)...
--
HTH...

Jim Thomlinson


"Gary''s Student" wrote:

From Bob Phillips:

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



--
Gary's Student


"jutlaux" wrote:

Is there a way to get the the user name of the current user who is logged on
to a PC so that it can be inputted into a cell?


Chip Pearson

Loged On User Name
 
Environ("UserName") is a whole pile easier and based on a long drawn out

True enough, but I've encountered situations in which an environment
variable is not returned when accessed by name. It works if you use the
number (but those numbers aren't the same across PCs and you have to split
apart the result), but not by name. I use the API version myself, but have
a wrapper function similar to Bob's in my standard library module, which is
imported into every project I work on.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"Jim Thomlinson" wrote in message
...
Environ("UserName") is a whole pile easier and based on a long drawn out
discussion on the topic (posted some time back) there is really no great
advantage to using the API that you posted... That being said the code you
posted works just fine (I used to use it myself)...
--
HTH...

Jim Thomlinson


"Gary''s Student" wrote:

From Bob Phillips:

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



--
Gary's Student


"jutlaux" wrote:

Is there a way to get the the user name of the current user who is
logged on
to a PC so that it can be inputted into a cell?




Jim Thomlinson

Loged On User Name
 
Thanks... Good to know... I actually have a module that I used to use but
gave up on it in favour of "Environ"... When I do any consulting i will be
sure to ue the API...
--
HTH...

Jim Thomlinson


"Chip Pearson" wrote:

Environ("UserName") is a whole pile easier and based on a long drawn out


True enough, but I've encountered situations in which an environment
variable is not returned when accessed by name. It works if you use the
number (but those numbers aren't the same across PCs and you have to split
apart the result), but not by name. I use the API version myself, but have
a wrapper function similar to Bob's in my standard library module, which is
imported into every project I work on.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"Jim Thomlinson" wrote in message
...
Environ("UserName") is a whole pile easier and based on a long drawn out
discussion on the topic (posted some time back) there is really no great
advantage to using the API that you posted... That being said the code you
posted works just fine (I used to use it myself)...
--
HTH...

Jim Thomlinson


"Gary''s Student" wrote:

From Bob Phillips:

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



--
Gary's Student


"jutlaux" wrote:

Is there a way to get the the user name of the current user who is
logged on
to a PC so that it can be inputted into a cell?






All times are GMT +1. The time now is 02:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com