By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

Windows ID & Username

P: 77
Hi Guys !!

Is there any way we can extract the Windows ID & Username associated with it using VBA?

For example, if my Windows ID is "kumar1" and I have a Username "yaaara" associated with it, what is the way to extract both? I am able to extract the ID but not the username..

Any Ideas please?

Thanks !
Nov 2 '08 #1
Share this Question
Share on Google+
8 Replies


puppydogbuddy
Expert 100+
P: 1,923
If you can get the Windows ID, then see this thread about getting the username:

http://bytes.com/forum/thread191309.html

The code below credited to rsmith@cta.org, explains how to use a UDF in an ms access module to obtain the username from Windows 2000:

If not already done so, create a new module in the Modules section.

Paste this statement into the General Declarations section of the module:
Expand|Select|Wrap|Line Numbers
  1. Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
  2.     "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Paste this code:
Expand|Select|Wrap|Line Numbers
  1. Function fWin2KUserName() As String
  2.     Dim lngLength As Long, lngX As Long
  3.     Dim strUserName As String
  4.     strUserName = String$(254, 0)
  5.     lngLength = 255
  6.     lngX = apiGetUserName(strUserName, lngLength)
  7.     If lngX <> 0 Then
  8.         fWin2KUserName = Left$(strUserName, lngLength - 1)
  9.     Else
  10.         fWin2KUserName = "unknown"
  11.     End If
  12. End Function
You can run this function (fWin2KUserName) from anywhere within your application. The best way is to set some variable to be equal to the function:
Expand|Select|Wrap|Line Numbers
  1.     Dim strUser as String
  2.     strUser = fWin2KUserName
Nov 2 '08 #2

puppydogbuddy
Expert 100+
P: 1,923
I just found out that there is an even easier way to get the username....use the Environ() function.


MsgBox "Network Name = " & Environ("username")
MsgBox "Computer Name = " & Environ("computername")

See this link for further details:

http://office.microsoft.com/en-us/ac...288311033.aspx
Nov 2 '08 #3

risk32
P: 98
With this concept, is there a way to extract the email address from Outlook associated with the username?
Nov 3 '08 #4

puppydogbuddy
Expert 100+
P: 1,923
With this concept, is there a way to extract the email address from Outlook associated with the username?
I am not aware of any publically available code to extract the email address for a given username. There is code on the MSDN public forum to extract email addresses from an email folder at this link:
http://forums.microsoft.com/msdn/Sho...70080&SiteID=1

I am sure there are third party add-ins that extract email addresses based on the username.
Nov 3 '08 #5

P: 77
Thanks for your replies guys.. However, using both the methods as advised returns the same result i.e. the windows ID and not the Name associated with that ID.

Let me try to explain the problem again.

In a domain based environment (Office Network), one gets a user ID and their names are associated with the IDs

If I press Ctrl+Alt+Del after logging in, I get a screen that tells me <yaaara> is logged in as <Domain Name>\kumar1
Also I get the logon date and options to logoff etc.

Using the above mentioned methods, I get "kumar1" but I am looking to extract "yaaara"

I hope I was able to explain properly.

Thanks.
Nov 4 '08 #6

puppydogbuddy
Expert 100+
P: 1,923
You described everything clearly, but did not identify the enviromental variable name that you are looking for. Is it one of the environmental variables listed below? I have already tried with username and computername....and you said neither of them give you what you want.

List of common environment variables.

ALLUSERSPROFILE
APPDATA
AVENGINE
CLIENTNAME
CommonProgramFiles
COMPUTERNAME
ComSpec
FP_NO_HOST_CHECK
HOMEDRIVE
HOMEPATH
INCLUDE
INOCULAN
LIB
LOGONSERVER
NUMBER_OF_PROCESSORS
OS
Path
PATHEXT
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
ProgramFiles
SESSIONNAME
SystemDrive
SystemRoot
TEMP
TMP
USERDOMAIN
USERNAME
USERPROFILE
VS71COMNTOOLS
WecVersionForRosebud.FF0
windir

Courtesy of Rob Cooper, MS
Custom environment variables could also be created and vary by system. You can write a loop to enumerate them as shown below:
Code:
--------------------------------------------------------------------------------

Expand|Select|Wrap|Line Numbers
  1. Sub test()    
  2. Dim i As Integer    
  3. Dim stEnviron As String    
  4. For i = 1 To 50        ' get the environment variable        
  5.     stEnviron = Environ(i)        ' see if there is a variable set        
  6.     If Len(stEnviron) > 0 Then            
  7.           Debug.Print i, Environ(i)        
  8.    Else            
  9.           Exit For        
  10.    End If    
  11. Next
  12. End Sub
Nov 4 '08 #7

P: 77
Unfortunately, none of these environment variables gives me the desired output.. several of these point towards the lan ID but none of these point to the associated name..

Not sure how else to describe the problem in more clarity :(
Nov 5 '08 #8

puppydogbuddy
Expert 100+
P: 1,923
Unfortunately, none of these environment variables gives me the desired output.. several of these point towards the lan ID but none of these point to the associated name..

Not sure how else to describe the problem in more clarity :(
I understand what you want, but I don't know what the variable name is called. In your first post you stated that the variable you were looking for was the username, but you say Environs("username") doesn't give it to you. So if it isn't username, what is the name of the variable that you are looking for?
Nov 5 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.