Results 1 to 7 of 7
  1. #1
    bignate is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2013
    Posts
    47

    Question How To Get Email from Active Directory

    Okay so I know how to send an email using access but I wanted to know if anyone knows how to get the emails address that are in the active directory

    Thanks

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Maybe you can get some info using the techniques in this thread.
    https://www.accessforums.net/code-re...ame-38386.html

  3. #3
    bignate is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2013
    Posts
    47
    Thanks, I ended up using this:

    Public Function EmailAd(Optional ByVal LoginName As String) As String

    Const adCmdText = 1

    Dim ADConn As Object 'ADODB Connection
    Dim ADCommand As Object 'ADODB Command
    Dim ADUser As Object
    Dim DomainDN As String

    If LoginName = vbNullString Then
    LoginName = CreateObject("wscript.network").UserName
    End If

    DomainDN = CreateObject("LDAP://rootDSE").Get("defaultNamingContext")

    Set ADConn = CreateObject("ADODB.Connection")
    With ADConn
    .Provider = "ADsDSOObject"
    .Open "Active Directory Provider"
    End With

    Set ADCommand = CreateObject("ADODB.Command")
    With ADCommand
    .ActiveConnection = ADConn
    .CommandType = adCmdText
    .CommandText = "SELECT mail FROM 'LDAP://" & DomainDN & "' WHERE objectCategory='User' AND sAMAccountName ='" & LoginName & "'"
    End With

    Set ADUser = ADCommand.Execute

    With ADUser
    EmailAd = !mail
    End With

    ADConn.Close

    Set ADConn = Nothing
    Set ADCommand = Nothing
    Set ADUser = Nothing

    End Function
    However I can only get it to show my email. I cant get it to show a departments email or even a list of all emails in the active directory
    Last edited by bignate; 12-06-2013 at 07:38 AM. Reason: I put in the wrong code

  4. #4
    bignate is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2013
    Posts
    47
    I tried changing the select statement to this to try and show the IT departments email or even every member of the IT Departments email. However ended up showing one person email in the department

    CommandText = "SELECT mail FROM 'LDAP://" & DomainDN & "' WHERE department = 'Information Technology'"

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Just a wild guess. Maybe you can loop through with a variant. Something like.

    Dim varAddress as variant
    Dim strAnswer as string

    for each varAddress in ADCommand.CommandText

    strAnswer = varAddress
    debug.print strAnswer

    next

    If it debugs a series if indexes then maybe.

    dim intCount as integer

    for intCount = 0 to ADCommand.CommandText
    strAnswer = ADCommand.CommandText
    debug.print strAnswer
    next

  6. #6
    bignate is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2013
    Posts
    47
    Thanks. Where in my code would I put this?

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    After you assign .CommandText

    Just after
    .CommandText = "SELECT mail FROM 'LDAP://" & DomainDN & "' WHERE objectCategory='User' AND sAMAccountName ='" & LoginName & "'"
    End With

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Combo box to pull from active directory
    By JackieEVSC in forum Forms
    Replies: 2
    Last Post: 12-06-2011, 08:39 AM
  2. MS Active Directory
    By pkelly in forum Access
    Replies: 9
    Last Post: 10-21-2011, 08:26 AM
  3. access+Active directory
    By cpcp in forum Access
    Replies: 6
    Last Post: 11-15-2010, 02:30 AM
  4. Active Directory coding
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 08-28-2010, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums