Solved

outlook email from active directory

Posted on 2015-01-06
7
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
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…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

733 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