Results 1 to 3 of 3

Thread: Outlook shared contacts/groups/distribution lists

  1. #1
    Lively Member
    Join Date
    May 12
    Posts
    91

    Question Outlook shared contacts/groups/distribution lists

    Hi,

    I'm wanting to be able to click a button in my form and display the contacts I see in my outlook, perhaps in a ListView.

    With a possible filter applied, e.g. if TextBox1.Text = "@example.com" then it will only fill the ListView with contacts containing that string.

    Or if TextBox1.Text = "Dist1" then the ListView would be filled with contacts from a distribution list named "Dist1".

    If anyone can help me with this please, or maybe suggest a better way of achieving this.

    Thanks.

  2. #2

  3. #3
    Lively Member
    Join Date
    May 12
    Posts
    91

    Question Re: Outlook shared contacts/groups/distribution lists

    Kevin,

    Thanks for your reply.

    I had tried searching Google, but to no avail.

    The code samples that you found I have tested and do not do anything as far as I can tell.

    1st:
    vb Code:
    1. ' Create Outlook application.
    2.         Dim oApp As Outlook.Application = New Outlook.Application()
    3.  
    4.         ' Get NameSpace and Logon.
    5.         Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
    6.         oNS.Logon("", "", Missing.Value, Missing.Value) ' TODO:
    7.  
    8.         ' Get the first contact from the Contacts folder.
    9.         Dim cContacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
    10.         Dim oItems As Outlook.Items = cContacts.Items
    11.  
    12.         Dim oCt As Outlook.ContactItem
    13.  
    14.         Try
    15.  
    16.             oCt = oItems.GetFirst()
    17.  
    18.  
    19.             ' Display some common properties.
    20.             Console.WriteLine(oCt.FullName)
    21.             Console.WriteLine(oCt.Title)
    22.             Console.WriteLine(oCt.Birthday)
    23.             Console.WriteLine(oCt.CompanyName)
    24.             Console.WriteLine(oCt.Department)
    25.             Console.WriteLine(oCt.Body)
    26.             Console.WriteLine(oCt.FileAs)
    27.             Console.WriteLine(oCt.Email1Address)
    28.             Console.WriteLine(oCt.BusinessHomePage)
    29.             Console.WriteLine(oCt.MailingAddress)
    30.             Console.WriteLine(oCt.BusinessAddress)
    31.             Console.WriteLine(oCt.OfficeLocation)
    32.             Console.WriteLine(oCt.Subject)
    33.             Console.WriteLine(oCt.JobTitle)
    34.  
    35.         Catch
    36.  
    37.             Console.WriteLine("an error occurred")
    38.  
    39.         Finally
    40.  
    41.             ' Display
    42.             'oCt.Display(True)
    43.  
    44.             ' Log off.
    45.             oNS.Logoff()
    46.  
    47.             ' Clean up.
    48.             oApp = Nothing
    49.             oNS = Nothing
    50.             oItems = Nothing
    51.             oCt = Nothing
    52.  
    53.         End Try

    For the logon information when it said "Profile" I wasn't sure if this meant the profile name of the pst by default "Outlook", or my account logon name "firstname" or my full name "firstname surname" or my email address "name@company.com" none seemed to work, so I read that you can use "oNS.Logon("", "", Missing.Value, Missing.Value)" and also that these may not work if outlook is already running..? Well outlook will most likely be running when this program is used, so the code would need to work with outlook already running and also without...

    This displayed no information in the console window, but I have definitely got contacts and groups in my Address book.

    I tried the 2nd example:
    vb Code:
    1. ' Create Outlook application.
    2.         Dim oApp As Outlook.Application = New Outlook.Application()
    3.  
    4.         ' Get Mapi NameSpace and Logon.
    5.         Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
    6.         oNS.Logon("", "", Missing.Value, Missing.Value) ' TODO:
    7.  
    8.         ' Get Global Address List.
    9.         Dim oDLs As Outlook.AddressLists = oNS.AddressLists
    10.         Dim oGal As Outlook.AddressList = oDLs.Item("Global Address List")
    11.         Console.WriteLine(oGal.Name)
    12.  
    13.         ' Get a specific distribution list.
    14.         ' TODO: Replace the distribution list with a distribution list that is available to you.
    15.         Dim sDL As String = "TestDL"
    16.         Dim oEntries As Outlook.AddressEntries = oGal.AddressEntries
    17.         ' No filter available to AddressEntries
    18.         Dim oDL As Outlook.AddressEntry = oEntries.Item(sDL)
    19.  
    20.         Console.WriteLine(oDL.Name)
    21.         Console.WriteLine(oDL.Address)
    22.         Console.WriteLine(oDL.Manager)
    23.  
    24.         ' Get all of the members of the distribution list.
    25.         oEntries = oDL.Members
    26.         Dim oEntry As Outlook.AddressEntry
    27.         Dim i As Integer
    28.  
    29.         For i = 1 To oEntries.Count
    30.             oEntry = oEntries.Item(i)
    31.             Console.WriteLine(oEntry.Name)
    32.  
    33.             ' Display the Details dialog box.
    34.             'oDL.Details(Missing.Value)
    35.         Next
    36.  
    37.         ' Log off.
    38.         oNS.Logoff()
    39.  
    40.         ' Clean up.
    41.         oApp = Nothing
    42.         oNS = Nothing
    43.         oDLs = Nothing
    44.         oGal = Nothing
    45.         oEntries = Nothing
    46.         oEntry = Nothing

    In the "for" statement I get an error at "oEntry = oEntries.Item(i)" stating:

    NullReferenceException was unhandled
    Object reference not set to an instance of an object.

    I don't know what is wrong, my Global Address List is full of contacts, have even set up a new TestDL group to practice with, and it doesn't seem to retrieve any contacts...

    Any help would be greatly appreciated on this, really want to get this solved.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •