Solved

C# Search and Open contacts in outlook

Posted on 2015-02-11
6
110 Views
Last Modified: 2015-03-17
I am coding an application to integrate with our phone system. However, I am having issues finding any documentation on how to search the current outlook profile that is open and if it finds the match for a phone number it pulls open the contact in outlook. If it does not find the match it open a new contact to create.

Looking for some pointers on this. I know it can be done I jsut cant figure it out in the SDK.
0
Comment
Question by:desiredforsome
  • 4
  • 2
6 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40605054
HI,

to read the contacts for their numbers try

using System;
using Outlook = Microsoft.Office.Interop.Outlook;
class Program
{

    static void Main()
    {
        var outlookApplication = new Microsoft.Office.Interop.Outlook.Application();

        Outlook.NameSpace mapiNamespace = outlookApplication.GetNamespace("MAPI");
        Outlook.MAPIFolder contacts = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

        foreach (Outlook.ContactItem foundContact in contacts.Items)
        {
            if (foundContact.BusinessTelephoneNumber != null)
            {
                Console.WriteLine(foundContact.FullName);
                Console.WriteLine(foundContact.BusinessTelephoneNumber);
                Console.WriteLine();
            }
        }
   }
}

Open in new window

Regards
0
 

Author Comment

by:desiredforsome
ID: 40605520
I know that I can read the contacts this way but I want it to popup the contact file within outlook when it finds it.
0
 

Author Comment

by:desiredforsome
ID: 40605535
Ah wait i think i see it contactitem.display();
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:desiredforsome
ID: 40605580
So I got part working. I got it to pull up the contact if it exists but I cant get ti to pull up a blank new contact if it does not exist. I even tried testing it with just having a message box show up.

It seems to be ignoring my else

        if(e.Channel.ToUpper().StartsWith("SIP/200"))
            {
               
                astTrayNotify.Icon = SystemIcons.Application;
                astTrayNotify.BalloonTipTitle = "Incomming Call";
                astTrayNotify.BalloonTipText = e.CallerIdName.ToString() + Environment.NewLine + e.CallerIdNum.ToString();
                astTrayNotify.ShowBalloonTip(3000);
                var outlookapplication = new Microsoft.Office.Interop.Outlook.Application();
                Outlook.NameSpace mapiNamespace = outlookapplication.GetNamespace("MAPI");
             Outlook.ContactItem contactitem =  (Outlook.ContactItem)outlookapplication.CreateItem(Outlook.OlItemType.olContactItem);
             Outlook.MAPIFolder contacts = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
           
                foreach (Outlook.ContactItem foundContact in contacts.Items)
                {
                    if (foundContact.BusinessTelephoneNumber == e.CallerIdNum.ToString())
                    {
                       
                        foundContact.Display();
                    }
                    else
                    {
                        MessageBox.Show("TEST");
                    }
                   
                }
               
               
             
               
            }
0
 

Author Comment

by:desiredforsome
ID: 40605607
I have it working with the following code although I dont belive this is the clean way of doing things.

      if(e.Channel.ToUpper().StartsWith("SIP/200"))
            {
                
                astTrayNotify.Icon = SystemIcons.Application;
                astTrayNotify.BalloonTipTitle = "Incomming Call";
                astTrayNotify.BalloonTipText = e.CallerIdName.ToString() + Environment.NewLine + e.CallerIdNum.ToString();
                astTrayNotify.ShowBalloonTip(3000);
                var outlookapplication = new Microsoft.Office.Interop.Outlook.Application();
                Outlook.NameSpace mapiNamespace = outlookapplication.GetNamespace("MAPI");
             Outlook.ContactItem contactitem =  (Outlook.ContactItem)outlookapplication.CreateItem(Outlook.OlItemType.olContactItem);
             Outlook.MAPIFolder contacts = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
            
                foreach (Outlook.ContactItem foundContact in contacts.Items)
                {
                    if (foundContact.BusinessTelephoneNumber == e.CallerIdNum.ToString())
                    {
                        found = 1;
                        foundContact.Display();
                        
                    }
                    
                    
                }
               if(found ==0)
               {
                   contactitem.FullName = e.CallerIdName.ToString();
                   contactitem.BusinessTelephoneNumber = e.CallerIdNum.ToString();
                   contactitem.Display();
                   found = 0;

               }
                if(found==1)
                {
                    found = 0;
                }
               
             
                
            }
     

Open in new window

0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40605688
pls try

if(found ==0)
               {
                Outlook.ContactItem newContact = (Outlook.ContactItem)
                outlookApplication.CreateItem(Outlook.OlItemType.olContactItem);
                try
                {
                    newContact.FirstName = "Jo";
                    newContact.LastName = "Berry";
                    newContact.Email1Address = "somebody@example.com";
                    newContact.CustomerID = "123456";
                    newContact.PrimaryTelephoneNumber = "(425)555-0111";
                    newContact.MailingAddressStreet = "123 Main St.";
                    newContact.MailingAddressCity = "Redmond";
                    newContact.MailingAddressState = "WA";
                    newContact.Save();
                    newContact.Display(true);
                }
                catch
                {
                    Console.WriteLine("The new contact was not saved.");
                }
}
                found = 0;

Open in new window

EDIT closing If bracket
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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