Drag mailitem from "Advanced Find" window in Outlook and drop on Winforms?

483 Views Asked by At

Most code samples for dragging a MailItem from Outlook and dropping on a widows form involve a reference to Outlook's ActiveExplorer window ...

Here's an example

Dim objMI As Microsoft.Office.Interop.Outlook.MailItem

For Each objMI In objOL.ActiveExplorer.Selection()
    Dim strFile As String = _
                IO.Path.Combine("c:\temp", _
                                (objMI.Subject + ".msg").Replace(":", ""))
    lblFile.Text += strFile + Environment.NewLine
    objMI.SaveAs(strFile)
Next

... however this produces unexpected results if dragging from the Advanced Find popup window.

What happens is your C# or VB code grabs the selected email from the main Outlook window not the one you've actually selected from the Advanced Find list of emails. Unless the user is alert he won't even notice he's got the wrong email.

Is there any solution to this?

1

There are 1 best solutions below

0
On

You could determine if their is not an ActiveWindow() (no active explorer or inspector in use) and then leverage the IDataObject interface instead of ActiveExplorer.Selection(). For our plugin - we just chose not to support the Advanced Find for drag-n-drop operations, but this is not always an option.

For an example using Win32 IDataObject - download this Code Project Outlook Drag-n-Drop example. For some reason Microsoft chose not to expose a VSTO interface wrapper around the Advanced Find window. This issue still exists in Office 2013.