web 2.0


How to get the username of the current user logged onto the PC

This quick tip shows you how to get the username using an API function. This function can be applied to all of the office suite, ie, Excel, Word, Access, Outlook, etc...

Declare Function GetUserName Lib "advapi32.dll" Alias _
 "GetUserNameA" ( _
    ByVal lpBuffer As String, _
     ByRef nSize As Long) As Long

Function showUsername() As String
    Dim username    As String
    Dim N           As Integer

    username = String(255, " ")
    GetUserName username, 255
    N = InStr(1, username, Chr(0)) - 1
    showUsername = Mid(username, 1, N)

End Function

Tags: , , , , , , , ,

Microsoft Office | Microsoft Office - VBA

Comments

Comments are closed