Outlook 对象模型 ContactItem 不会删除
我为此烦恼不已。我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉 C# 语法,但我怀疑这是因为您在创建副本时要添加到 Items 集合中。我会这样做:
在 foreach 循环开始之前,检查 ContactItems 的计数:
创建副本后,再次检查 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:
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.