简体   繁体   English

从C#搜索公共Outlook联系人文件夹

[英]Searching public Outlook contacts folder from C#

We have a large public contacts folder in Outlook called Global Contacts, I'd like to be able to search through it and return a number of results that match certain criteria, ideally wildcard-style. 我们在Outlook中有一个名为“公共联系人”的大型公共联系人文件夹,我希望能够在其中搜索并返回符合某些条件(理想情况下为通配符样式)的多个结果。

Eg if someone puts "je" in the 'name' textbox, it will return all contacts whose names contain 'je'. 例如,如果有人在“名称”文本框中输入“ je”,它将返回名称包含“ je”的所有联系人。 This may be coupled as an AND with a companyname textbox. 这可以与AND一起与公司名称文本框耦合。

Most of the examples I've seen are either in VB, or are concerned with doing this form a web app - I'm doing a winforms app, and every machine has Outlook 2002 installed (yeah, I know, update long overdue). 我见过的大多数示例都在VB中,或者与通过Web应用程序进行此操作有关-我正在使用Winforms应用程序,并且每台计算机都安装了Outlook 2002(是的,我知道,应该早点更新)。

Can anyone point me in the right direction? 谁能指出我正确的方向? Some code would be nice as a place to start. 一些代码将是一个不错的起点。

Cheers 干杯

I ended up doing this: 我最终这样做:

            Microsoft.Office.Interop.Outlook._Application objOutlook; //declare Outlook application
            objOutlook = new Microsoft.Office.Interop.Outlook.Application(); //create it
            Microsoft.Office.Interop.Outlook._NameSpace objNS = objOutlook.Session; //create new session
            Microsoft.Office.Interop.Outlook.MAPIFolder oAllPublicFolders; //what it says on the tin
            Microsoft.Office.Interop.Outlook.MAPIFolder oPublicFolders; // as above
            Microsoft.Office.Interop.Outlook.MAPIFolder objContacts; //as above
            Microsoft.Office.Interop.Outlook.Items itmsFiltered; //the filtered items list
            oPublicFolders = objNS.Folders["Public Folders"];
            oAllPublicFolders = oPublicFolders.Folders["All Public Folders"];
            objContacts = oAllPublicFolders.Folders["Global Contacts"];

            itmsFiltered = objContacts.Items.Restrict(strFilter);//restrict the search to our filter terms

Then just looping through itmsFiltered to add it to an ObjectListView. 然后只需遍历itmsFiltered即可将其添加到ObjectListView。 Hopefully this will be of use to someone else looking to do the same - it took me a while to cobble this together from various sources. 希望这对其他希望做到这一点的人有用-我花了一段时间才从各种来源将其拼凑起来。

to find contacts folder you can iterate items of olFolderContacts. 要查找联系人文件夹,您可以迭代olFolderContacts的项目。 Here is the code 这是代码

using System;
using Microsoft.Office.Interop.Outlook;
using Application = Microsoft.Office.Interop.Outlook.Application;

namespace RyanCore
{
    public class Loader
    {
        public static ContactsViewModel LoadModel(Application objOutlook)
        {
            var viewModel = new ContactsViewModel();

            MAPIFolder fldContacts = objOutlook.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
            foreach (object obj in fldContacts.Items)
            {
                if (obj is _ContactItem)
                {
                    var contact = (_ContactItem) obj;
                    viewModel.Contacts.Add(new Contact(contact.FirstName + " " + contact.LastName, contact.Email1Address));
                }
                else if (obj is DistListItem)
                {
                    var distListItem = (DistListItem) obj;
                    var contactGroup = new ContactGroup(distListItem.Subject);

                    viewModel.Groups.Add(contactGroup);
                    for (Int32 i = 1; i <= distListItem.MemberCount; i++)
                    {
                        Recipient subMember = distListItem.GetMember(i);
                        contactGroup.Contacts.Add(new Contact(subMember.Name, subMember.AddressEntry.Address));
                    }
                }
            }
            return viewModel;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM