Private Declare Function GetUserName Lib "advapi32.dll" _ Alias "GetUserNameA" (ByVal lpBuffer As String, nSize _ As Long) As LongThen, you can use above function in a generic function like this:
Public Function RetrieveUserName() As String ' A const to specify the max length of the user name. Const MaxLen = 50 ' Dim some variables to store the info. ' A string variable to hold the user name. This variable ' must be buffered in order for the function to work. Dim strName As String ' Used to contain the length of the name retrieved. Dim lngRetVal As Long ' Buffer the string with spaces. strName = Space$(MaxLen) ' Get the user name and assign it to strName; ' lngRetVal will hold the length of the user name. lngRetVal = GetUserName(strName, MaxLen) ' Now we must trim the remaining buffering spaces. strName = Trim$(strName) ' Because GetUserName returns a value with a trailing ' null termination character, we must remove that ' character. strName = Left$(strName, Len(strName) - 1) RetrieveUserName = strName End FunctionHTH.