Excel

Getting the Username

Ease of Use

Easy

Version tested with

2000, 2002 

Submitted by:

Justinlabenne

Description:

Shows different ways to obtain the username 

Discussion:

This code shows 4 different way to obtain the current username. Useful if you need a consistent way to tell who is currently logged in. Application.Username only returns the name used for the application, and in many companies the name can be a default "Company User" or something similiar. Using api, the wscript.network object, or the Environ function, you can obtain the actual name of the user who is logged into Windows. The example file contains example for each: api, environ, wscript, and the application. Another way of getting the username is listed here: http://vbaexpress.com/kb/getarticle.php?kb_id=234 

Code:

instructions for use

			

Option Explicit 'api call for obtaining the username Private Declare Function GetUserName& Lib "advapi32.dll" Alias "GetUserNameA" _ (ByVal lpBuffer As String, _ nSize As Long) Public Function WindowsUserName() As String ' --------------------------------------------- ' Function to extract the name: ' --------------------------------------------- Dim szBuffer As String * 100 Dim lBufferLen As Long lBufferLen = 100 If CBool(GetUserName(szBuffer, lBufferLen)) Then WindowsUserName = Left$(szBuffer, lBufferLen - 1) Else WindowsUserName = CStr(Empty) End If End Function Sub GetTheNameAPI() ' Deliver the name via msgbox ' Uses the api call above & the Function MsgBox "Network username is: " & WindowsUserName End Sub Sub GetTheNameAPP() ' Deliver the name via msgbox ' Not reliable, returns the office username, not Windows MsgBox "Application username is: " & Application.UserName End Sub Sub GetTheNameENVIRON() ' Deliver the name via msgbox ' Same as the api version, MsgBox "Environ username is: " & Environ("USERNAME") End Sub Sub GetTheNameNETWORK() ' Deliver the name via msgbox ' Same as the api and Environ versions. ' Useful if you can't use Environ or API calls, ' ie. vbscript Dim objNet As Object On Error Resume Next Set objNet = CreateObject("WScript.NetWork") MsgBox "Network username is: " & objNet.UserName Set objNet = Nothing End Sub

How to use:

  1. Open an Excel Workbook
  2. Copy the code
  3. Press Alt + F11 to open the Visual Basic Editor (VBE)
  4. Select INSERT > MODULE from the menubar
  5. Paste code into the right pane
  6. Press Alt+Q to return to Excel
  7. Save workbook before any other changes
 

Test the code:

  1. Go to TOOLS > MACRO > MACROS
  2. When the dialog appears, select (GetTheNameAPI)
  3. Press Run
  4. Go to TOOLS > MACRO > MACROS
  5. When the dialog appears, select (GetTheNameAPP)
  6. Press Run
  7. Go to TOOLS > MACRO > MACROS
  8. When the dialog appears, select (GetTheNameENVIRON)
  9. Press Run
 

Sample File:

Getting the Username.zip 8.52KB 

Approved by mdmackillop


This entry has been viewed 452 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express