All pastes #1630004 Raw Edit

VBA: Get Username from PC

public text v1 · immutable
#1630004 ·published 2009-10-19 15:32 UTC
rendered paste body
Option Compare Database
Option Explicit
Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserName Lib "ADVAPI32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long


Public Function GetCurrentUserName() As String
On Error GoTo Err_GetCurrentUserName
Dim lpBuff As String * 25
Dim ret As Long, Username As String
  ret = GetUserName(lpBuff, 25)
  Username = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
  GetCurrentUserName = Username & ""

Exit_GetCurrentUserName:
   Exit Function

Err_GetCurrentUserName:
       MsgBox Err.Description
       Resume Exit_GetCurrentUserName
End Function