Get the logged on user with vba

Q

Q

Hello you all,

I have a word document with some code behind it.
In the code, I want to know the user who is logged on to
the machine.
Can this be done from within VBA in a word document?

Can anyone provide me with a way of achieving this?

Regards,

Q
 
K

Karl E. Peterson

Q said:
I have a word document with some code behind it.
In the code, I want to know the user who is logged on to
the machine.
Can this be done from within VBA in a word document?

Absolutely...

Private Declare Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Const UNLEN As Long = 256 ' Maximum username length

Private Function CurrentUserName() As String
Dim Buffer As String
Dim nLen As Long
Const NameLength = UNLEN + 1

Buffer = Space$(NameLength)
nLen = Len(Buffer)
If GetUserName(Buffer, nLen) Then
CurrentUserName = Left$(Buffer, nLen - 1)
End If
End Function

Later... Karl
 
R

Robert Paulsen

You can also try "Environ("username")". The function is part of the VBA
library, so your project should already have a reference to it.
 
K

Karl E. Peterson

Robert said:
You can also try "Environ("username")". The function is part of the
VBA library, so your project should already have a reference to it.

Sure, but the user is free to alter that string, so it's not generally the
preferred method. (Not sure it's available on the 9x platform either?)
 

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

Top