Search code examples
vb.netdrag-and-dropoutlook-addinoutlook-2010

Interop.Outlook doesn't clear selected mails at drag and drop


I have a control which I can drop mail items on, works fine but I can't get it to clear the selection / items.

For example: I drag and drop mail 1 --> mail 1 is in my list I delete mail 1 from my list go back to Outlook and drag and drop mail 2
Mail 2 appears in my list but mail 1 is also revived! I've found a lot of postings about Marshal.ReleaseComObject but I guess I'm not doing it in the right way?

Specs: VS2010, 4.0 framework. Windows 7 OS, Outlook 2010

Here's part of my code:

The call to my Save method:

ElseIf e.Data.GetDataPresent("FileGroupDescriptor") Then
    Try
        Dim SafeSaveMethod As New dlgCallSaveMails(AddressOf SaveMailsFromSelection)
        Me.BeginInvoke(SafeSaveMethod, Me.FileData.Pad)

The Save method:

Private Sub SaveMailsFromSelection(_path As String)
    ' File uit Outlook
    Dim x As Integer
    Dim xitmndx As Integer = 0
    Dim DestFile As String
    Dim oOutLook As New Outlook.Application
    Dim oExplorer As Outlook.Explorer
    Dim oSelection As Outlook.Selection
    Dim strFile As String

    oExplorer = oOutLook.ActiveExplorer
    oSelection = oExplorer.Selection
    Dim currentFolder As MAPIFolder = oExplorer.CurrentFolder
    Dim folders As Folders = currentFolder.Folders


    Try
        For Each mitem As Object In oSelection
            xitmndx += 1

            Dim mi As Microsoft.Office.Interop.Outlook.MailItem = TryCast(mitem, Microsoft.Office.Interop.Outlook.MailItem)

                        mi.SaveAs(_path & "\" & String.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", mi.CreationTime) & "-" & CleanInput(mi.Subject) & ".msg", Outlook.OlSaveAsType.olMSG)

                Marshal.ReleaseComObject(mi)
                mi = Nothing
        Next

    Catch ex As System.Exception
        WriteError2EventLog("Error picDropZone_DragDrop 4: " & ex.ToString)
        MsgBox(Err.Description, MsgBoxStyle.Exclamation, "mycontrol")
    Finally
        Marshal.ReleaseComObject(oExplorer)
        Marshal.ReleaseComObject(oSelection)
        Marshal.ReleaseComObject(currentFolder)
        Marshal.ReleaseComObject(folders)
        Marshal.FinalReleaseComObject(oExplorer)
    End Try
End Sub

I also tried oExplorer.ClearSelection() but as I can tell from the count property it doesn't clear at all


Solution

  • After spending hours reading up on different solutions to this problem, which ends up beeing a bug in Outlooks way of handling the enter-event when moving over a control that can handle drag & drop in another program, I found out that you can fix it with one single line of code and that is something that should be spread!

    Microsoft uses the clipboard to store, among other things, information about the selection. The class that is used by Outlook for this purpose is hidden behind the key named RenPrivateMessages. It can't be used because they won't release the interface, but by reading it you clear the lock on the selection.

    So, all you have to do in the drop-event in your code is to add this row (given that your EventArg is named e):

    e.data.GetData("RenPrivateMessages");