Solved

Use Late Binding To Read Address Book in Outlook

Posted on 2008-10-04
9
362 Views
Last Modified: 2012-05-05
Hi,

I am trying to turn this into a late binding function to read the exchange address book within Outlook for vb.net using VS2003 but I suspect on any version. The reason I am using this is my boss does not want to have to import the reference for Outlook. Thanks for any help. This works if not using late binding. Thanks for any help.
 
Sub Main()
 

        ' TODO: Replace My DL Name with a valid distribution list name.

        Dim sDLName As String = "My DL Name"
 

        ' Create an Outlook application.

        Dim oApp As Outlook._Application = New Outlook.Application()
 

        ' Get the MAPI namespace.

        Dim oNS As Outlook.NameSpace = oApp.Session
 

        ' Get the Global Address List.

        Dim oALs As Outlook.AddressLists = oNS.AddressLists

        Dim oGal As Outlook.AddressList = oALs.Item("Global Address List")

        Console.WriteLine(oGal.Name)
 

        ' Get a specific Distribution List.

        Dim oEntries As Outlook.AddressEntries = oGal.AddressEntries

        ' Reference the Distribution List by name.

        Dim oDL As Outlook.AddressEntry = oEntries.Item(sDLName)
 

        Console.WriteLine(oDL.Name)

        Console.WriteLine(oDL.Address)
 

        ' Get all the members of the Distribution List.

        oEntries = oDL.Members

        Dim oEntry As Outlook.AddressEntry

        Dim i As Integer
 

        For i = 1 To oEntries.Count

            oEntry = oEntries.Item(i)

            Console.WriteLine(oEntry.Name)

        Next
 

        ' Clean up.

        oEntry = Nothing

        oEntries = Nothing

        oGal = Nothing

        oALs = Nothing

        oNS = Nothing

        oApp = Nothing

    End Sub

Open in new window

0
Comment
Question by:scorpion53061
  • 6
  • 3
9 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 22641397
Try this....
    Sub Main()
 

        ' TODO: Replace My DL Name with a valid distribution list name.

        Dim sDLName As String = "My DL Name"
 

        ' Create an Outlook application.

        Dim oApp As Object = CreateObject("Outlook.Application")
 

        ' Get the MAPI namespace.

        Dim oNS As Object = oApp.Session
 

        ' Get the Global Address List.

        Dim oALs As Object = oNS.AddressLists

        Dim oGal As Object = oALs.Item("Global Address List")

        Console.WriteLine(oGal.Name)
 

        ' Get a specific Distribution List.

        Dim oEntries As Object = oGal.AddressEntries

        ' Reference the Distribution List by name.

        Dim oDL As Object = oEntries.Item(sDLName)
 

        Console.WriteLine(oDL.Name)

        Console.WriteLine(oDL.Address)
 

        ' Get all the members of the Distribution List.

        oEntries = oDL.Members

        Dim oEntry As Object

        Dim i As Integer
 

        For i = 1 To oEntries.Count

            oEntry = oEntries.Item(i)

            Console.WriteLine(oEntry.Name)

        Next
 

        ' Clean up.

        oEntry = Nothing

        oEntries = Nothing

        oGal = Nothing

        oALs = Nothing

        oNS = Nothing

        oApp = Nothing

    End Sub

Open in new window

0
 

Author Comment

by:scorpion53061
ID: 22641412
Ok this is getting me somewhere but will have an error here for you a second.

First, how to suppress the alert?
Second I don't want ot see the address book. I want ot read the entries. For some reason it is showing me the entries dialog.

Thank you much for your help!
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22641433
scorpion53061,

I can't test at the moment (no Outlook), but I see no reason why it would act any differently to when you were using early binding. Did the alert popup before?

Wayne
0
 

Author Comment

by:scorpion53061
ID: 22641463
Ok here is the error (in picture file) and code as it stands now.

' Create an Outlook application.
        Dim oApp As Object = CreateObject("Outlook.Application")

        ' Get the MAPI namespace.
        Dim oNS As Object = oApp.Session

        ' Get the Global Address List.
        Dim oALs As Object = oNS.AddressLists
        Dim oGal As Object = oALs.Item("Global Address List")
        Debug.WriteLine(oGal.Name)

        ' Get a specific Distribution List.
        Dim oEntries As Object = oGal.AddressEntries
        ' Reference the Distribution List by name.
        Dim oDL As Object = oEntries.Item(1)

        Debug.WriteLine(oDL.Name)
        Debug.WriteLine(oDL.Address)

        ' Get all the members of the Distribution List.
        oEntries = oDL.Members

        Dim oEntry As Object
        Dim i As Integer

        For i = 0 To oEntries.Count - 1
            oEntry = oEntries.Item(i)
            Debug.WriteLine(oEntry.Name)
        Next

        ' Clean up.
        oEntry = Nothing
        oEntries = Nothing
        oGal = Nothing
        oALs = Nothing
        oNS = Nothing
        oApp = Nothing

Picture.jpg
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:scorpion53061
ID: 22641468
No this error did not appear when using early binding. Again thank you for your help.
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22641484
Do these 2 lines write something?

    Debug.WriteLine(oDL.Name)
    Debug.WriteLine(oDL.Address)
0
 

Author Comment

by:scorpion53061
ID: 22641498
Yes they do.
0
 

Author Comment

by:scorpion53061
ID: 22642019
Well if I do the loop onGal it will show users but there appears no way to overcome the security prompt issue which makes it useless to me. But I will give you the points for your efforts. And thank you.
0
 

Author Closing Comment

by:scorpion53061
ID: 31503093
It doesnt overcome the security prompt issue but in no way is that the fault of the individual working with me.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Web appliction 7 47
Data is not showing from images 15 37
Run a .bat file wich is inside a .vbs file 8 22
VB.NET Repostiory Pattern 7 8
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

24 Experts available now in Live!

Get 1:1 Help Now