How To Get Email from Active Directory

I apologize, I thought I was using the code you provided.

I changed mine to match yours exactly. Now, I am getting a new error message. The error message is "Object doesn't support this property or method". When I click on Debug, it highlights this line of code:
Code:
 .Source = "SELECT mail FROM 'LDAP://" & DomainDN & "' WHERE objectCategory='User' AND department ='" & DeptName & "'"
 
This is the code I'm trying to use.

Code:
Option Compare Database

Public Function DeptEmails(ByVal DeptName As String) As Object
 
Const adOpenStatic = 3
Const adUseClient = 3
 
Dim DomainDN As Object
Dim someName As String
Dim ADConn As Object    'ADODB Connection
Dim rs As Object        'ADODB.Recordset
 
    Set DomainDN = CreateObject("LDAP://RootDSE")
    someName = DomainDN.Get("DefaultNamingContext")
 
    Set ADConn = CreateObject("ADODB.Connection")
        With ADConn
            .Provider = "ADsDSOObject"
            .Open "Active Directory Provider"
        End With
 
    Set rs = CreateObject("ADODB.Recordset")
 
        With rs
            .ActiveConnection = ADConn
            .Source = "SELECT mail FROM 'LDAP://" & DomainDN & "' WHERE objectCategory='User' AND department ='" & DeptName & "'"
            .CursorType = adOpenStatic
            .CursorLocation = adUseClient
            .Open
            .ActiveConnection = Nothing
        End With
 
    ADConn.Close
 
    Set DeptEmails = rs
 
    Set ADConn = Nothing
    Set rs = Nothing
 
End Function

Then I'm trying to test it in my immediate window using ?DeptEmails("US Field Service*")
 
You really have to make a little bit of modification, now that we are storing the information of the naming context in the variable someName, you need to use that there. Not the Object.
Code:
.Source = "SELECT mail FROM 'LDAP://" & [COLOR=Red][B]someName [/B][/COLOR]& "' WHERE objectCategory='User' AND department ='" & DeptName & "'"
 
I changed the line of code as you suggested. Now, it says I have a "Type Mismatch".
 
Code:
.Source = "SELECT mail FROM 'LDAP://" & somename & "' WHERE objectCategory='User' AND department
 
Giving this a bump...I found it yesterday, altered it a little and it is working like a charm. Thanks to all who have contributed.Trying to make it work on a non-default server is proving difficult but I will get there...
 

Users who are viewing this thread

Back
Top Bottom