Kev Hunter
Kev Hunter

Reputation:

Iterate through a contacts properties using outlook

I want to iterate through a contacts properties and add those that contain the word "Number" to a list with the value, i tries using reflection but it doesn't work.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Office.Interop.Outlook;

namespace DuplicateNumbers
{
    public class ContactService
    {
        public ContactItem Contact { get; private set; }

        private IDictionary<string,string> phoneNumbers = new Dictionary<string, string>();

        public ContactService(ContactItem contact)
        {
            Contact = contact;
        }

        public IDictionary<string,string> PhoneNumbers
        {
            get
            {
                if(phoneNumbers.Count == 0)
                {
                    PopulatePhoneNumbers();
                }
                return phoneNumbers;
            }
        }

        private void PopulatePhoneNumbers()
        {
            _ContactItem ci = Contact as _ContactItem;
            MemberInfo[] members = ci.GetType().FindMembers(MemberTypes.All, BindingFlags.Instance, (m,criteria) => true, null);
            foreach (var info in members)
            {
                if(info.Name.Contains("Number"))
                {
                    phoneNumbers.Add(info.Name,info.Value);
                }
                Console.WriteLine(info);
            }
        }
    }
}

Upvotes: 0

Views: 1334

Answers (3)

Sanjay Karia
Sanjay Karia

Reputation: 318

This seems to be able to access the Outlook.ContactItem properties. Enumerating Outlook ContactItem properties

Upvotes: 0

Paige Watson
Paige Watson

Reputation: 1214

Try using MAPI CDO.

Here's a microsoft site that might get you started: How to use CDO to read MAPI Addresses

Here's some MAPI Blogs to help as well:

Upvotes: 1

IgorM
IgorM

Reputation: 1068

Of cause it doesn't work - it's a COM object. You should use the properties from CDO space.

Upvotes: 0

Related Questions