none
How to check whether a contact exists in outlook? RRS feed

  • Question

  • Hai all,

    how can i check whether contact exists in outlook.If contact exists the code should display a error message????plz help

    Monday, June 13, 2011 7:31 AM

Answers

  • This code will check for an existing contact in the default Contacts folder using the FullName property, but I think that comparison test is prone to failure or mistakes.
     
    The reason is if someone were to enter my name for a contact the first name could be entered as Ken, or Kenneth, or Kenny. The test would not find the alternate possibilities. Other names could have nicknames or variations of the name entered. Usually a mix of name + address + company might be used to be more comprehensive.
     
    // nameToTest is a string that's the new contact full name
    Outlook.NameSpace ns = ThisAddin.Application.GetNameSpace("MAPI);
    Outlook.Folder contacts = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    Outlook.Items items = contacts.Items;
    string filter = "[FullName] = '" + nameToTest + "'";
    Outlook.ContactItem existingContact = null;
    existingContact = items.Find(filter) as Outlook.ContactItem;
    if (existingContact != null)
    {
        MessageBox.Show("This contact exists already");
    }
     
    Bear in mind that this code snippet does no error handling.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Arun Raju" <=?utf-8?B?QXJ1biBSYWp1?=> wrote in message news:983851e9-558c-452f-a0e3-02f430a315c4...

    if contact exists, a message box should popup displaying some message.else there should some code to add contact.

    for example.

    if a contact named "arun" is already thre.if someone tries to add "arun" again. a message box should popup saying that "contact is already there".


    Ken Slovak MVP - Outlook
    • Marked as answer by Bruce Song Monday, June 20, 2011 1:36 PM
    Wednesday, June 15, 2011 3:20 PM

All replies

  • What version of Outlook? Is this in a VSTO addin? What language?
     
    What information do you have about the contact to see if it does exist?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Arun Raju" <=?utf-8?B?QXJ1biBSYWp1?=> wrote in message news:bee54c24-461d-4fbd-8af5-0d48919daba5...

    Hai all,

    how can i check whether contact exists in outlook.If contact exists the code should display a error message????plz help


    Ken Slovak MVP - Outlook
    Monday, June 13, 2011 9:17 PM
  • its 2007 outlook.its is a VSTO addin.language c#.
    Tuesday, June 14, 2011 3:31 AM
  • What information do you have about the contact to see if it does exist?
    What criteria do you want to use to determine if a contact exists? Do you want to use name or company or address or a combination of property values?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Arun Raju" <=?utf-8?B?QXJ1biBSYWp1?=> wrote in message news:5dae65ad-8aef-4804-8691-ba810f040bf5...
    its 2007 outlook.its is a VSTO addin.language c#.

    Ken Slovak MVP - Outlook
    Tuesday, June 14, 2011 2:01 PM
  • if contact exists, a message box should popup displaying some message.else there should some code to add contact.

    for example.

    if a contact named "arun" is already thre.if someone tries to add "arun" again. a message box should popup saying that "contact is already there".

    Wednesday, June 15, 2011 3:47 AM
  • This code will check for an existing contact in the default Contacts folder using the FullName property, but I think that comparison test is prone to failure or mistakes.
     
    The reason is if someone were to enter my name for a contact the first name could be entered as Ken, or Kenneth, or Kenny. The test would not find the alternate possibilities. Other names could have nicknames or variations of the name entered. Usually a mix of name + address + company might be used to be more comprehensive.
     
    // nameToTest is a string that's the new contact full name
    Outlook.NameSpace ns = ThisAddin.Application.GetNameSpace("MAPI);
    Outlook.Folder contacts = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    Outlook.Items items = contacts.Items;
    string filter = "[FullName] = '" + nameToTest + "'";
    Outlook.ContactItem existingContact = null;
    existingContact = items.Find(filter) as Outlook.ContactItem;
    if (existingContact != null)
    {
        MessageBox.Show("This contact exists already");
    }
     
    Bear in mind that this code snippet does no error handling.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Arun Raju" <=?utf-8?B?QXJ1biBSYWp1?=> wrote in message news:983851e9-558c-452f-a0e3-02f430a315c4...

    if contact exists, a message box should popup displaying some message.else there should some code to add contact.

    for example.

    if a contact named "arun" is already thre.if someone tries to add "arun" again. a message box should popup saying that "contact is already there".


    Ken Slovak MVP - Outlook
    • Marked as answer by Bruce Song Monday, June 20, 2011 1:36 PM
    Wednesday, June 15, 2011 3:20 PM
  • thanks a lot sir....
    Thursday, June 16, 2011 6:52 AM