Logged on User Name

R

Rich M

Is there a way to find the logged on user name and computer name from a Word
VBA application?

Using VSTO the system.environment command gives me this but I can't find how
to do it from VBA.

Rich
(e-mail address removed)
 
K

Karl E. Peterson

Rich said:
Is there a way to find the logged on user name and computer name from a Word
VBA application?

Indented to highlight wordwrap...

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA"
(ByVal lpBuffer As String, nSize As Long) As Long

Private Const UNLEN As Long = 256 ' Maximum username length
Private Const CNLEN As Long = 15 ' Maximum computer name length

Private Function CurrentMachineName() As String
Dim Buffer As String
Dim nLen As Long
Const NameLength = CNLEN + 1
Buffer = Space$(NameLength)
nLen = Len(Buffer)
If GetComputerName(Buffer, nLen) Then
CurrentMachineName = Left$(Buffer, nLen)
End If
End Function

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
Using VSTO the system.environment command gives me this but I can't find how
to do it from VBA.

Eeeeewwww! Well, if you want to rely on volatile methods, you could do that too.

Environ$("COMPUTERNAME")
Environ$("USERNAME")

Ew, ew, ew! :-|
 

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