如何查找特定 Outlook 联系人子文件夹的内容?

How do I find the contents of a specific Outlook Contacts subfolder?

我需要提取 Outlook 联系人中指定(按名称)子文件夹中列出的联系人(员工)。我已经研究了好几天,并尝试了我在互联网上可以找到的所有代码片段,但无济于事。我看到了有关如何可以完成的建议,但其中 none 确实有效。

这是我的 运行 代码。您可以按原样将其放入 Visual Studio 的控制台应用程序中。我可以找到我的“联系人”文件夹的内容,我可以找到文件夹列表,但不知道如何获取子文件夹的内容。

我们正在使用 Outlook 365。

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

namespace OutlookTest
{
    public class OutlookDataRetriever
    {
        public static int Main(string[] args)
        {
            try
            {
                Outlook.Application oApp = new Outlook.Application();


                Console.WriteLine("\n\nShow Contacts folders:");
                Outlook.MAPIFolder folderContacts = oApp.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
                foreach (Outlook.MAPIFolder subfolder in folderContacts.Folders)
                {
                    Console.WriteLine("folder::: " + subfolder.Name + " ::: " + subfolder.FolderPath);
                }

                Console.WriteLine("\n\nShow members of Contacts folder:");
                Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                Outlook.Items contactsFolder = fldContacts.Items;
                foreach (var contact in contactsFolder)
                {
                    if (contact is Outlook.ContactItem)
                    {
                        Outlook.ContactItem thisContact = (Outlook.ContactItem)contact;
                        Console.WriteLine("contact::: " + thisContact.FullName + " ::: " + thisContact.Email1DisplayName);
                    }
                    else
                    {
                        Console.WriteLine("I'm guessing this is a folder, but can't figure out how to cast it to access its contents.");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
            return 0;
        }
    }
}

我的参考资料: snapshot of the solution explorer showing references

这是我得到的输出:

Show Contacts folders:
folder::: {06967759-274D-40B2-A3EB-D7F9E73727D7} ::: \KNye@myOrganization.com\Contacts\{06967759-274D-40B2-A3EB-D7F9E73727D7}
folder::: {A9E2BC46-B3A0-4243-B315-60D991004455} ::: \KNye@myOrganization.com\Contacts\{A9E2BC46-B3A0-4243-B315-60D991004455}
folder::: Companies ::: \KNye@myOrganization.com\Contacts\Companies
folder::: GAL Contacts ::: \KNye@myOrganization.com\Contacts\GAL Contacts
folder::: Kristy Nye[1] ::: \KNye@myOrganization.com\Contacts\Kristy Nye[1]
folder::: Kristy Nye ::: \KNye@myOrganization.com\Contacts\Kristy Nye
folder::: Recipient Cache ::: \KNye@myOrganization.com\Contacts\Recipient Cache
folder::: PeopleCentricConversation Buddies ::: \KNye@myOrganization.com\Contacts\PeopleCentricConversation Buddies
folder::: Kristy's Addresses ::: \KNye@myOrganization.com\Contacts\Kristy's Addresses
folder::: Organizational Contacts ::: \KNye@myOrganization.com\Contacts\Organizational Contacts


Show members of Contacts folder:
contact::: Matt Smith ::: Matt Smith (ESmith@myOrganization.com)
contact::: Kristy J Nye ::: Kristy J Nye (KNye@myOrganization.com)
I'm guessing this is a folder, but can't figure out how to cast it to access its contents.

这是我的联系人文件夹的屏幕截图: Screenshot of Contacts folder

这是我的文件夹“ThisIsATestGroup”的内容:Contents of "ThisIsATestGroup" folder

我的objective: 我想查询“查找“ThisIsATestGroup”中的所有联系人,并返回:

如何只获取此特定子文件夹中的联系人???谢谢!

你真正需要的是

Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.MAPIFolder fldEmployees = fldContacts.Folders["Employees"];
foreach (var item in fldEmployees.Items)
{
    Outlook.ContactItem contact = item as Outlook.ContactItem;
    if (contact != null)
    {
       Console.WriteLine("contact::: " + contact .FullName + " ::: " + contact .Email1DisplayName);
    }
    else
    {
       Outlook.DistListItem distList = item as Outlook.DistListItem;
       if (distList != null)
       {
          Console.WriteLine("distList ::: " + distList.DLName);
       }
    }
 }                   

在扯头发和咬牙切齿之后,我找到了自己的答案。这里是:

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

namespace OutlookTest
{
    public class OutlookDataRetriever
    {
        public static int Main(string[] args)
        {
            string myFolder = "ThisIsATestGroup";
            try
            {
                Console.WriteLine("Show members of '" + myFolder + "' list:");

                Outlook.Application oApp = new Outlook.Application();

                /* Get a list of items in the contactsfolder  */
                Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                Outlook.Items contactsFolder = fldContacts.Items;

                /* Find the contacts folder with the name I'm looking for.  MS documentation calls this a DistListItem.  Baffled me for a long time because their UI calls it a contact group. */
                string filter = "[Subject] = '" + myFolder + "'";
                var targetFolder = contactsFolder.Find(filter);
                Outlook.DistListItem myDistList = (Outlook.DistListItem)targetFolder;
                Console.WriteLine("myDistList::: " + myDistList.Subject);

                /* List all the members in my list */
                for (int i = 1; i <= myDistList.MemberCount; i++) // Index starts at 1, not 0 !!!
                {
                    var aMember = myDistList.GetMember(i);
                    Console.WriteLine("      member::: " + i.ToString() + " " + aMember.Name + " ::: " + aMember.Address);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
            return 0;
        }
    }
}

在寻找上述解决方案的过程中,我发现了另一种解决方案,它的额外好处是允许您查看联系人文件夹中的所有内容。两种解决方案 return 都是正确的结果,但我怀疑最上面的解决方案运行得更快。

namespace OutlookTest
{
    public class OutlookDataRetriever
    {
        public static int Main(string[] args)
        {
            try
            {
                Outlook.Application oApp = new Outlook.Application();

                Console.WriteLine("Show members of 'ThisIsATestGroup' list:");
                Outlook.MAPIFolder fldContactsX = (Outlook.MAPIFolder)oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                Outlook.Items contactsFolder = fldContactsX.Items;
                foreach (var contact in contactsFolder)
                {
                    if (contact is Outlook.ContactItem)
                    {
                        Outlook.ContactItem thisContact = (Outlook.ContactItem)contact;
                        Console.WriteLine("contact::: " + thisContact.FullName + " ::: " + thisContact.Email1DisplayName);
                    }
                    else
                    {
                        if (contact is Outlook.DistListItem)
                        {
                            Outlook.DistListItem thisDistList = (Outlook.DistListItem)contact;
                            Console.WriteLine("distList::: " + thisDistList.Subject);

                            if (thisDistList.Subject == "ThisIsATestGroup")
                            {

                                for (int i = 0; i < thisDistList.MemberCount; i++)
                                {
                                    var aMember = thisDistList.GetMember(i + 1);
                                    Console.WriteLine("      member::: " + i.ToString() + " " + aMember.Name + " ::: " + aMember.Address);
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("Should never reach this line.");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
            return 0;
        }
    }
}

尽情享受吧!