Get NT Username

S

Steven Pugh

Hi,

I trying to create a spreadsheet that when you go in, depending on what user
you are, you can see different worksheets sheets.

I figure I can do this, if I could first determine the user that is logged
on via on our NT network.

I there a formula / function in ExcelXP that can do this


Regards
Steven
 
B

Bob Phillips

No built-in, but you can do it with a UDF.

Here is a function to do 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

The worksheet cell could then use

=UserName()



--

HTH

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

Steven Pugh

Building Functions is my Achilles heal in excel and I'm not sure how to
implement this


Regards
Steven
 
B

Bob Phillips

On a worksheet, hit Alt-F11. This takes you into the VBE. On the Insert
menu, select module. In the window that pops up, paste the code that I
supplied, and then you can use it as I showed.

--

HTH

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

David Fixemer

Bob,

Cool trick & works great! However, I'm confused as to
what the if statement is doing? When does it do the Left$
and when doesn't it? Is there information about the
various librarys/Controls such as advapi32.dll?

David
 
B

Bob Phillips

David,

The If statement is serving two purposes. It is issuing and checking the
call to the API in one statement. If GeteUserName is not successful, it
returns a 0, which will resolve to false in that statement. If it is
successful, it retunes 1, and the following statement strips off the null
terminator which is a common need when working with APIs.

To get more details about APIs, I suggest checking out www.allapi.net,
and/or getting Dan Appleman's guide to the Win32 API.

--

HTH

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

Steven Pugh

Fantastic cheers

Steven



Bob Phillips said:
David,

The If statement is serving two purposes. It is issuing and checking the
call to the API in one statement. If GeteUserName is not successful, it
returns a 0, which will resolve to false in that statement. If it is
successful, it retunes 1, and the following statement strips off the null
terminator which is a common need when working with APIs.

To get more details about APIs, I suggest checking out www.allapi.net,
and/or getting Dan Appleman's guide to the Win32 API.

--

HTH

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

Bob Phillips

That's odd, David became Steven in the reply<VBG>

--

HTH

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