Get login name to spred sheet

  • Thread starter Thread starter Raj
  • Start date Start date
R

Raj

Hi,

I need to get login name into a worksheet as a value. I am
using Windows 2000 as operating system.

thanks

Raj
 
Hi Raj

In a module:

Option Explicit 'top of module

Private Declare Function GetUserName Lib "ADVAPI32.DLL" _
Alias "GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long

Private Function LogonUser()
Dim S As String
Dim N As Long
Dim Res As Long

S = String$(200, 0)
N = 199
Res = GetUserName(S, N)
LogonUser = Left(S, N - 1)
End Function

Sub EnterUsername()
Sheets(1).Range("A1").Value = LogonUser
End Sub
 
Hi Raj,

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

Put this in a standard code module. 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)
 

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

Back
Top