How To Get Email from Active Directory (1 Viewer)

LadyDi

Registered User.
Local time
Yesterday, 19:34
Joined
Mar 29, 2007
Messages
894
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 & "'"
 

LadyDi

Registered User.
Local time
Yesterday, 19:34
Joined
Mar 29, 2007
Messages
894
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*")
 

pr2-eugin

Super Moderator
Local time
Today, 03:34
Joined
Nov 30, 2011
Messages
8,494
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 & "'"
 

LadyDi

Registered User.
Local time
Yesterday, 19:34
Joined
Mar 29, 2007
Messages
894
I changed the line of code as you suggested. Now, it says I have a "Type Mismatch".
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 12:34
Joined
Jan 20, 2009
Messages
12,851
Code:
.Source = "SELECT mail FROM 'LDAP://" & somename & "' WHERE objectCategory='User' AND department
 

NauticalGent

Ignore List Poster Boy
Local time
Yesterday, 22:34
Joined
Apr 27, 2015
Messages
6,319
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

Top Bottom