在C#中接收(拖动并)删除Outlook联系人?

时间:2009-07-30 14:32:20

标签: c# wpf outlook contactitem

我正在开发一个需要对用户的Outlook联系人执行某些处理的应用程序。我目前正在通过迭代MAPIFolder.Items.Restrict(somefilter)的结果来访问Outlook联系人列表,该结果可以在Microsoft.Office.Interop.Outlook中找到。

在我的应用程序中,我的用户需要选择几个联系人来应用某个操作。我想添加一个功能,允许用户从Outlook拖动联系人并将其放在UI中的某个ListBox上(我在WPF中工作,但这可能是更通用的问题)。

我是C#和WPF的新手 - 我怎么能:

  1. 在ListBox上接收已删除的项目
  2. 验证它是ContactItem(或包装ContactItem的东西)
  3. 将已删除的项目投射到ContactItem,以便我可以处理它
  4. 由于

3 个答案:

答案 0 :(得分:6)

我尝试使用TextBox(在实践中与ListBox没有区别)。

摘要:

在所有Outlook联系人中搜索作为文本拖动的收件人。 此处的搜索基于此人的FullName。

(多个)条件:

拖动联系人时,它必须在Outlook中选中时显示FullName。唯一的问题是当两个人拥有相同的全名!!如果是这种情况,您可以尝试通过组合ContactItem属性并在拖动的文本中搜索它们来为人找到唯一标识符。

private void textBox1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetData("Text") != null)
    {                
        ApplicationClass app;
        MAPIFolder mapif;
        string contactStr;

        contactStr = e.Data.GetData("Text").ToString();

        app = new ApplicationClass();                

        mapif = app.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderContacts);                

        foreach (ContactItem tci in mapif.Items)
        {
            if (contactStr.Contains(tci.FullName))
            {
                draggedContact = tci; //draggedContact is a global variable for example or a property...
                break;
            }                    
        }

        mapif = null;

        app.Quit;
        app = null;
        GC.Collect();
    }
}

当然这个代码是有组织优化的,它只是解释使用的方法。

您可以尝试将Explorer.Selection属性与GetData(“Text”)结合使用[以确保它来自Outlook,或者您可以在DragOver事件中使用GetData(“Object Descriptor”),解码内存流,搜索对于“outlook”,如果没有找到取消拖动操作]为什么不拖动多个联系人!

private void textBox1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetData("Text") != null)
    {
        ApplicationClass app;
        Explorer exp;
        List<ContactItem> draggedContacts;                
        string contactStr;

        contactStr = e.Data.GetData("Text").ToString();

        draggedContacts = new List<ContactItem>();

        app = new ApplicationClass();
        exp = app.ActiveExplorer();
        if (exp.CurrentFolder.DefaultItemType == OlItemType.olContactItem)
        {
            if (exp.Selection != null)
            {
                foreach (ContactItem ci in exp.Selection)
                {
                    if (contactStr.Contains(ci.FullName))
                    {
                        draggedContacts.Add(ci);
                    }
                }
            }
        }

        app = null;
        GC.Collect();
    }
}

答案 1 :(得分:1)

Outlook联系人在删除时支持以下格式:

(0): "RenPrivateSourceFolder"
(1): "RenPrivateMessages"
(2): "FileGroupDescriptor"
(3): "FileGroupDescriptorW"
(4): "FileContents"
(5): "Object Descriptor"
(6): "System.String"
(7): "UnicodeText"
(8): "Text"

该列表中最有趣的一个(对我来说)是对象描述符,然后引导我到一个有类似声音问题的人:

http://bytes.com/topic/visual-basic-net/answers/527320-drag-drop-outlook-vb-net-richtextbox

在这种情况下,它们会检测到它是一个Outlook丢弃,然后使用Outlook对象模型来检测当前选择的内容,暗示它必须是当前的放置源。

答案 2 :(得分:0)

您可能做的是接受.wpf应用中的拖放事件,然后从outlook中获取所选项目并将其拉入您的应用程序。

<强>更新

将Outlook PIA引用添加到您的应用程序。

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.Explorer activeExplorer = app.ActiveExplorer();
Microsoft.Office.Interop.Outlook.Selection currentSelection = activeExplorer.Selection;

然后,您可以遍历currentSelection集合以查看用户拖动的内容。