Pucho
Pucho

Reputation: 380

Programmatically Search for a Specific Contact in Outlook using WinForm

I found this code here to search for Outlook contacts using C# winform. I'm having a hard time understanding why the code is not working as is. VS2015 is telling me "'Myapp' does not contain a definition for 'Application' and no extension method 'Application' accepting a first argument of type 'Myapp' could be found (are you missing a using directive or an assembly reference?)"

Here is the code:

 private void FindContactEmailByName(string firstName, string lastName)
    {
        Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI"); //VS does not like this line, particularly "Application"
        Outlook.MAPIFolder contactsFolder =
            outlookNameSpace.GetDefaultFolder(
            Microsoft.Office.Interop.Outlook.
            OlDefaultFolders.olFolderContacts);
        Outlook.Items contactItems = contactsFolder.Items;
        try
        {
            Outlook.ContactItem contact =
                (Outlook.ContactItem)contactItems.
                Find(String.Format("[FirstName]='{0}' and "
                + "[LastName]='{1}'", firstName, lastName));
            if (contact != null)
            {
                contact.Display(true);
            }
            else
            {
                MessageBox.Show("The contact information was not found.");
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Upvotes: 0

Views: 933

Answers (1)

Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

the line Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");
throws me the same message.
I fixed your code and tested it.

please notice the declaration of the outlook object in the first 2 rows.
also in order to set contacts folder i have changed it from your original code to outlook.GetNamespace("MAPI").GetDefaultFolder(MyOutlook.OlDefaultFolders.olFolderContacts);

using MyOutlook= Microsoft.Office.Interop.Outlook;

  private void FindContactEmailByName(string firstName, string lastName)
        {
            Microsoft.Office.Interop.Outlook.Application outlook;
            outlook = new Microsoft.Office.Interop.Outlook.Application();

           MyOutlook.MAPIFolder contactsFolder =
           outlook.GetNamespace("MAPI").GetDefaultFolder(MyOutlook.OlDefaultFolders.olFolderContacts);
           MyOutlook.Items contactItems = contactsFolder.Items;
            try
            {
                MyOutlook.ContactItem contact =
                    (MyOutlook.ContactItem)contactItems.
                    Find(String.Format("[FirstName]='{0}' and "
                    + "[LastName]='{1}'", firstName, lastName));
                if (contact != null)
                {
                    contact.Display(true);
                }
                else
                {
                    MessageBox.Show("The contact information was not found.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Upvotes: 1

Related Questions