Microsoft KB Archive/161394

= VBA: Sample Code to Retrieve the Current User Name =

Article ID: 161394

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Visual Basic for Applications 5.0
 * Microsoft Excel 97 Standard Edition
 * Microsoft Word 97 Standard Edition
 * Microsoft Access 97 Standard Edition
 * Microsoft PowerPoint 97 Standard Edition

-



This article was previously published under Q161394



SUMMARY
The "More Information" section of this article contains sample Microsoft Visual Basic for Applications code that calls WNetGetUser (a Windows API function in the file Mpr.dll) to retrieve the user name used to establish a network connection.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

Sample Visual Basic Procedure
' Declare for call to mpr.dll. Declare Function WNetGetUser Lib "mpr.dll" _ Alias "WNetGetUserA" (ByVal lpName As String, _     ByVal lpUserName As String, lpnLength As Long) As Long

Const NoError = 0      'The Function call was successful

Sub GetUserName

' Buffer size for the return string. Const lpnLength As Integer = 255

' Get return buffer space. Dim status As Integer

' For getting user information. Dim lpName, lpUserName As String

' Assign the buffer size constant to lpUserName. lpUserName = Space$(lpnLength + 1)

' Get the log-on name of the person using product. status = WNetGetUser(lpName, lpUserName, lpnLength)

' See whether error occurred. If status = NoError Then ' This line removes the null character. Strings in C are null- ' terminated. Strings in Visual Basic are not null-terminated. ' The null character must be removed from the C strings to be used ' cleanly in Visual Basic. lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1) Else

' An error occurred. MsgBox "Unable to get the name." End End If

' Display the name of the person logged on to the machine. MsgBox "The person logged on this machine is: " & lpUserName

End Sub

