Link to home
Start Free TrialLog in
Avatar of scorpion53061
scorpion53061

asked on

Use Late Binding To Read Address Book in Outlook

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

ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of scorpion53061
scorpion53061

ASKER

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!
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
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
No this error did not appear when using early binding. Again thank you for your help.
Do these 2 lines write something?

    Debug.WriteLine(oDL.Name)
    Debug.WriteLine(oDL.Address)
Yes they do.
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.
It doesnt overcome the security prompt issue but in no way is that the fault of the individual working with me.