?
Solved

Reading Outlook Contact Details using C#

Posted on 2008-06-20
3
Medium Priority
?
1,742 Views
Last Modified: 2013-12-16
Hello,
i am using Outlook 2007 and C#.NET 2005 to search the contact from Outlook.
though i have developed the applciation and it is working fine but i have a problem with one of my laptop. in that i have installed office 2007.

i have tested this with office 2007 and also office 2003 on XP, Windows 2003 and Vista machines.
i am not able to understand the problem that i am getting. when i run my application on my laptop i get error "Object referecne not set to an instance of object".

i am attaching the Code snippet that i am using

in the code i am getting error between linenumber 101  and linenumber 104. linenumber is a variable which is assigend line number value to trace the linenumber where error occured.

Regards.
private void btnSearch_Click(object sender, EventArgs e)
{
     int linenumber=68;
     searchResultList.Clear();
     bool bloop = false;
     string owner="";
     OutLook.Items contactItems = null;
     try
     {
          linenumber=68;
          OutLook.Application outapp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
          OutLook.NameSpace outnamespace = outapp.GetNamespace("MAPI");
          OutLook.MAPIFolder contactsFolder = outnamespace.GetDefaultFolder (OutLook.OlDefaultFolders.olFolderContacts);
          contactItems = contactsFolder.Items;
          owner = outnamespace.CurrentUser.AddressEntry.Name;
          linenumber=74;
     }
     catch
     {
          MessageBox.Show("Either Outlook is not available or Outlook has reject your request for connection.", "Outlook Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          return;
     }
 
     try
     {
           if (contactItems.Count == 1 )
           {
                OutLook.ContactItem contact = (OutLook.ContactItem)contactItems.GetFirst();//  .GetNext();
                linenumber=87;
                if (contact.FirstName.ToUpper().StartsWith(txtSearchBox.Text.Trim().ToUpper()))
                {
                    linenumber = 90;
                    Result searchresult = new Result();
                    string name = contact.Title + " " + contact.FirstName + " " + contact.MiddleName + " " + contact.LastName;
                        searchresult.showResult(name, contact.CompanyName, contact.Email1Address, contact.MobileTelephoneNumber);
                    searchresult.ShowDialog();
                    linenumber = 95;
                }
           }
           else if (contactItems.Count > 1)
           {
               bool found = false;
               linenumber = 101;
               foreach (OutLook.ContactItem items in contactItems)
               {
                   if (items.FirstName.ToUpper().StartsWith(txtSearchBox.Text.Trim().ToUpper()))
                   {
                       linenumber = 104;
                       object matcheditems = items;
                       searchResultList.Add(matcheditems);
                       found = true;
                       linenumber = 109;
                    }
               }
               linenumber = 113;
               if (found == false)
               {
                   MessageBox.Show("No record found matching your criteria. Please verify specified search criteria.", "Outlook Contact", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
               else
               {
                   if (searchResultList.Count == 1)
                   {
                       linenumber = 122;
                       OutLook.ContactItem cntct = (OutLook.ContactItem)searchResultList[0];
                       Result searchresult = new Result();
                       string name = cntct.Title + " " + cntct.FirstName + " " + cntct.MiddleName + " " + cntct.LastName;
                       searchresult.showResult(name, cntct.CompanyName, cntct.Email1Address, cntct.MobileTelephoneNumber);
                       searchresult.ShowDialog();
                       linenumber = 128;
                   }
                   else
                   {
                       linenumber = 132;
                       MatchList matchlist= new MatchList(searchResultList);
                       matchlist.showMatchList();
                       matchlist.ShowDialog();
                       linenumber = 136;
                   }
               }
           }
           else
               MessageBox.Show("No record found matching your criteria. Please verify specified search criteria.", "Outlook Contact", MessageBoxButtons.OK, MessageBoxIcon.Information);      
      }
      catch (Exception ex)
      {
          MessageBox.Show("Exception after Line No. : " +linenumber.ToString()+" for "+ex.Message+"\nTotal match found "+searchResultList.Count.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);        
      } 
 }

Open in new window

0
Comment
Question by:atulmodi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 375 total points
ID: 21837246
I would suggest that either :

>> foreach (OutLook.ContactItem items in contactItems)

the collection contactItems has items in it that are null or :

>>if (items.FirstName.ToUpper().

you have items where the FirstName is null

surely you can tell this by setting a break point and looking in the stack ?

0
 
LVL 5

Expert Comment

by:rendaduiyan
ID: 21843186
at least, you can debug your  code and tell us which line throw such error.
0
 

Author Comment

by:atulmodi
ID: 21844137
Hello,
i have already tested it. i have tesed the EXE of four machines and it is working fine even at the place where i have not added any contact List.
only i am having problem on one of my laptop. and it is not a development machine.

Regards.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

777 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