Outlook 对象模型 ContactItem 不会删除

发布于 2024-11-26 05:40:09 字数 791 浏览 1 评论 0原文

我为此烦恼不已。我有一个 ac# 应用程序,在修改我的主要联系人之前我需要对其进行备份。但无论如何,该副本似乎都会保留下来。我通过目视检查 Outlook 中内容文件夹的内容来验证这一点。

我有一个像这样的简单测试用例...

Application outlookApplication = new Application();
NameSpace outlookNamespace = outlookApplication.GetNamespace("mapi");
outlookNamespace.Logon("", "", true, true);
MAPIFolder Folder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
MAPIFolder Folder2 = Folder.Folders["Test1"];
Items ContactItems = Folder2.Items;
foreach (ContactItem Contact in ContactItems)
{
    ContactItem Backup = (ContactItem)Contact.Copy();
    Backup.Delete();
    break;
}
outlookNamespace.Logoff();
outlookNamespace = null;

如果我尝试删除它两次,则会导致错误。 甚至尝试将其移动到已删除项目文件夹,但没有成功。展望2010。发生了什么?

编辑:解决方法:如果我创建一个新联系人并从原始联系人中填充,我可以将其删除。

I'm bedeviled by this. I have a c# application that I need to have a backup before I modify my main contact. But it seems that the copy, sticks around no matter what. I'm verifying this by visual check at the contents of my contents folder in Outlook.

I have a simple test case like so...

Application outlookApplication = new Application();
NameSpace outlookNamespace = outlookApplication.GetNamespace("mapi");
outlookNamespace.Logon("", "", true, true);
MAPIFolder Folder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
MAPIFolder Folder2 = Folder.Folders["Test1"];
Items ContactItems = Folder2.Items;
foreach (ContactItem Contact in ContactItems)
{
    ContactItem Backup = (ContactItem)Contact.Copy();
    Backup.Delete();
    break;
}
outlookNamespace.Logoff();
outlookNamespace = null;

If I try to delete it twice, it causes an error.
Even tried moving it to the deleted items folder, but no luck. Outlook 2010. What is going on?

EDIT: WORKAROUND: If I create a new contact and populate from the original, I can delete it just fine.

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

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

发布评论

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

评论(1

新一帅帅 2024-12-03 05:40:09

我不熟悉 C# 语法,但我怀疑这是因为您在创建副本时要添加到 Items 集合中。我会这样做:

在 foreach 循环开始之前,检查 ContactItems 的计数:

Items ContactItems = Folder2.Items;
' display ContactItems.Count here, is it Console.WriteLine(ContactItems.Count) ??

创建副本后,再次检查 ContactItems.Count。如果它增加了,那么您需要将循环更改为“For i = ContactItems.Count to 1 Step -1”类型的循环而不是foreach循环(抱歉,我只知道VB语法,我不知道等效的 C# 语法)。它必须是一个向后循环。

如果这不起作用,请创建一个副本并将其添加到另一个“联系人”文件夹中,这样就不会干扰您正在使用的文件夹的“项目”集合。这与您已经在做的事情类似。

I'm not familiar with C# syntax, but I suspect it's because you are adding to the Items collection when you create the copy. I would do this:

Before the start of the foreach loop, check the count of ContactItems:

Items ContactItems = Folder2.Items;
' display ContactItems.Count here, is it Console.WriteLine(ContactItems.Count) ??

After creating the copy, check the ContactItems.Count again. If it has increased, then you need to change your loop to a "For i = ContactItems.Count to 1 Step -1" type of loop instead of a foreach loop (sorry, I only know the VB syntax, I don't know the equivalent C# syntax). It has to be a backwards loop.

If that doesn't work, then create a copy and add it to another Contacts folder, that way it won't interfere with the Items collection of the folder you are working with. That is similar to what you are already doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文