Solved

outlook email from active directory

Posted on 2015-01-06
7
154 Views
Last Modified: 2015-01-12
Hello All,

Is it possible via vba if a excel sheet has a list of outlook users in a column, then for each of those contacts, i can get their names and positions from active directory or outlook server?

Thanks
0
Comment
Question by:Rayne
  • 4
  • 3
7 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 40533868
Assuming that by "outlook users" you mean a user name, then the answer is yes.  You can get the info from AD with something like this

Sub UpdateWorkbook()
    'On the next line, edit the path to your domain
    Const MY_DOMAIN = "LDAP://company.com"
    Dim excWks As Excel.Worksheet, adoCon As Object, adoRec As Object, lngRow As Long
    Set excWks = Application.ActiveSheet
    
    Set adoCon = CreateObject("ADODB.Connection")
    With adoCon
        .Provider = "ADsDSOObject"
        .CursorLocation = 3
        .Open "ADSI"
    End With
    
    For lngRow = 1 To excWks.UsedRange.Rows.Count
        Set adoRec = adoCon.Execute("SELECT sn,givenName,title FROM '" & MY_DOMAIN & "' Where objectClass='user' AND objectCategory='Person' AND samAccountName='" & excWks.Cells(lngRow, 1).Value & "'")
        If (Not adoRec.bof) And (Not adoRec.EOF) Then
            excWks.Cells(lngRow, 2) = adoRec.Fields("sn").Value
            excWks.Cells(lngRow, 3) = adoRec.Fields("givenName").Value
            excWks.Cells(lngRow, 4) = adoRec.Fields("title").Value
        End If
    Next

    adoRec.Close
    adoCon.Close
    Set adoRec = Nothing
    Set adoCon = Nothing
    Set excWks = Nothing
End Sub

Open in new window

0
 

Author Comment

by:Rayne
ID: 40533986
Hello BlueDevilFan,

I am getting an error when its executes
error5.bmp
0
 

Author Comment

by:Rayne
ID: 40533988
i gave  a sample existing user id like "ssamy"
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 76

Expert Comment

by:David Lee
ID: 40534062
Did you edit the MY_DOMAIN constant at the top of the script?
0
 

Author Comment

by:Rayne
ID: 40534110
yes I did to change it to my company..
0
 
LVL 76

Expert Comment

by:David Lee
ID: 40534648
Then the domain you entered must not be correct.  I tested the code using my domain and it worked correctly.
0
 

Author Comment

by:Rayne
ID: 40545856
thank you bluedevilfan :)
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Resolve DNS query failed errors for Exchange
Read this checklist to learn more about the 15 things you should never include in an email signature.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now