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

发布于 2024-10-21 15:42:56 字数 314 浏览 2 评论 0原文

我们在 Outlook 中有一个名为“全局联系人”的大型公共联系人文件夹,我希望能够搜索它并返回一些符合特定条件(最好是通配符样式)的结果。

例如,如果有人在“姓名”文本框中输入“je”,它将返回姓名包含“je”的所有联系人。这可以作为 AND 与公司名称文本框耦合。

我见过的大多数示例要么是用 VB 编写的,要么是与将其形成为 Web 应用程序有关 - 我正在做一个 winforms 应用程序,并且每台计算机都安装了 Outlook 2002(是的,我知道,更新早就该进行了)。

有人能指出我正确的方向吗?一些代码可以作为一个很好的起点。

干杯

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.

E.g. if someone puts "je" in the 'name' textbox, it will return all contacts whose names contain 'je'. This may be coupled as an AND with a companyname textbox.

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).

Can anyone point me in the right direction? Some code would be nice as a place to start.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲凉≈ 2024-10-28 15:42:56

我最终这样做了:

            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

然后只需循环 itmsFiltered 将其添加到 ObjectListView 中。希望这对其他想要做同样事情的人有用 - 我花了一段时间才从各种来源将其拼凑在一起。

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. 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.

我要还你自由 2024-10-28 15:42:56

要查找联系人文件夹,您可以迭代 olFolderContacts 的项目。这是代码

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;
        }
    }
}

to find contacts folder you can iterate items of 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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文