Get Email Id of currently logged in user

nithinkm

Board Regular
Joined
May 7, 2008
Messages
70
Hi anybody please send me the code or appropriate link to get Currently logged users Email-Id from Active directory.

Thanks in Advance
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
may be an issue if the alias on exchange does not equate to the NT username, also does your company use alias as valid address, ie:

jd@somewhere.com is valid address for john doe as is j.doe@somewhere.com

Test it, it may be that if you wish to assume alias = NT id that you can simply use the NT username + @somewhere.com as the basis for your email.

The NT id of active user can be ascertained in VBA using

Environ("username")
 
Upvote 0
Thanks LASW10 for the reply

But that will help a little bit, my actual scenario is I need the Email-id of the logged user, coz im using this Email-ID as a parameter for getting some unique ID from database. Is der any way to get the email id directly or by passing the username from Active Directory.

Please help
 
Upvote 0
The below could work assuming all running OL but requires OL object library be referenced in VBA

Code:
Function olCU()
Dim olNS As Outlook.NameSpace
Set olNS = Outlook.GetNamespace("MAPI")
olCU = olNS.CurrentUser.Address
End Function

To add an object library automatically you can use the GUID of the library itself

Code:
Set ID = ThisWorkbook.VBProject.References
ID.AddFromGuid "{00062FFF-0000-0000-C000-000000000046}", 9, 1

The references may need to change pending version etc...

Also, pending Office version you will also get OL VBA security warnings whilst running this code... you may be able to get this using an API call / CDO method.

Hopefully someone can provide a more elegant less dependent solution.
 
Upvote 0
Hi thanks again for the reply

im using this code to get user email address from the active directory. But it works fine in my machine and shows the email adress but when i install this in clients pc it doesnt work. Im giving the code .............
Anybody please help
Code
====

Set oRoot = GetObject("ldap://rootDSE")
sDomain = oRoot.Get("defaultNamingContext")
Set oDomain = GetObject("LDAP://" & sDomain)
sBase = "<" & oDomain.ADsPath & ">"
LoggedIn = Environ("username")
sFilter = "(&(objectCategory=person)(objectClass=user)(name=" & LoggedIn & "))"
sAttribs = "adsPath"
sDepth = "subTree"
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
Set Connection = New ADODB.Connection
Connection.Provider = "ADsDSOObject"
Connection.Open
Set RecSet = Connection.Execute(sQuery)
If Not RecSet.EOF And Not RecSet.BOF Then

Set User = GetObject(RecSet("adsPath"))

With User
On Error Resume Next
sAns = sAns & "Email Address: " & .EmailAddress & vbCrLf
MsgBox sAns
End With
End If
 
Upvote 0
You will need to ensure that each client has the ADO object library referenced (VBA).

See earlier note re: adding object references via GUID which can be invoked on workbook_open event - eg

Code:
Private Sub Workbook_Open()
On Error Resume Next
Set ID = ThisWorkbook.VBProject.References
ID.AddFromGuid "{00000205-0000-0010-8000-00AA006D2EA4}", 2, 5
End Sub

where 2,5 = major,minor version
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,871
Members
449,055
Latest member
excelhelp12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top