简体   繁体   中英

Receiving (dragged and) dropped Outlook contacts in C#?

I'm developing an application that needs to perform some processing on the user's Outlook contacts. I'm currently accessing the list of Outlook contacts by iterating over the result of MAPIFolder.Items.Restrict(somefilter) , which can be found in Microsoft.Office.Interop.Outlook .

In my application, my user needs to choose several contacts to apply a certain operation on. I would like to add a feature that will allow the user to drag a contact from Outlook and drop it on a certain ListBox in the UI (I work in WPF but this is probably is more generic issue).

I'm very new to C# and WPF - how can I:

  1. Receive a dropped item on a ListBox
  2. Verify it's a ContactItem (or something that wraps ContactItem)
  3. Cast the dropped item to a ContactItem so I can process it

Thanks

I tried this with a TextBox (no difference with a ListBox in practice).

Summary :

Searching in all outlook contacts for the one recieved dragged as text. The search here is based on the person's FullName.

condition(s):

When you drag a contact, it must show the FullName when selected in outlook. The only catch is when two persons have the same full names!! If it's the case you can try to find a unique identifier for a person by combining ContactItem properties and searching them in the dragged text.

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

of course this code is to be organized-optimized, it's only to explain the method used.

You can try using the Explorer.Selection property combined with GetData("Text") [to ensure it's coming from Outlook, or you can use GetData("Object Descriptor") in the DragOver Event, decode the memory stream, search for "outlook", if not found cancel the drag operation] And why not drag multiple contacts!

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

An Outlook contact, when dropped, supports the following formats:

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

The most interesting looking one on that list (for me) is Object Descriptor, which then led me to someone with a similar sounding problem:

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

Where it looks like in that case, they detect that it's an Outlook drop, and then use the Outlook object model to detect what's currently selected, with the implication that that must be the current drop source.

What you could probably do is accept the drag and drop event in your .wpf app and then get the selected item(s) from outlook and pull them into your application.

Update

Add the Outlook PIA references to you app.

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;

Then you can iterate over the currentSelection collection to see what the user has dragged over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM