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

Re: Get the logged on user with vba by Karl

Karl
Tue Mar 14 14:22:03 CST 2006

Q wrote:
> 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
--
Working without a .NET?
http://classicvb.org/



Re: Get the logged on user with vba by RobertPaulsen

RobertPaulsen
Wed Mar 15 14:50:29 CST 2006

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

"Karl E. Peterson" wrote:

> Q wrote:
> > 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
> --
> Working without a .NET?
> http://classicvb.org/
>
>
>

Re: Get the logged on user with vba by Karl

Karl
Wed Mar 15 16:08:19 CST 2006

Robert Paulsen wrote:
> 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?)
--
Working without a .NET?
http://classicvb.org/