Interop.Outlook不会在拖放时清除选定的邮件

时间:2012-12-30 11:41:27

标签: vb.net drag-and-drop outlook-addin outlook-2010

我有一个控件,我可以放下邮件,工作正常,但我无法清除选择/项目。

例如: 我拖放邮件1 - >邮件1在我的列表中 我从列表中删除邮件1返回Outlook并拖放邮件2
邮件2出现在我的列表中,但邮件1也恢复了! 我发现了很多关于Marshal.ReleaseComObject的帖子,但我想我没有以正确的方式做到这一点?

规格:VS2010,4.0框架。 Windows 7操作系统,Outlook 2010

这是我的代码的一部分:

Save方法的调用:

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

Save方法:

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

我也试过了oExplorer.ClearSelection(),但我可以从count属性中看出它根本不清楚

3 个答案:

答案 0 :(得分:6)

花了几个小时阅读这个问题的不同解决方案后,最终出现了一个错误的Outlook处理输入事件的方式,当移动一个可以处理阻力和控制的控件时放入另一个程序,我发现你可以用一行代码修复它,这是应该传播的东西!

Microsoft使用剪贴板存储有关选择的信息。 Outlook为此目的使用的类隐藏在名为RenPrivateMessages的键后面。它无法使用,因为它们不会释放界面,但通过阅读它可以清除选择上的锁定。

因此,您在代码中的drop-event中所要做的就是添加此行(假设您的EventArg名为e):

e.data.GetData(" RenPrivateMessages&#34);

答案 1 :(得分:1)

我正在网上搜索没有解决方法的解决方案来切换窗格。 在我找到适合我的解决方案后,我想在此分享。

最后的线索是使用主动资源管理器的RemoveFromSelection方法,cos Marshal.ReleaseComObject只是不清除选择。

Public Sub outlook_drop()
    Try
        get_outlook_application_explorer()
        If IsNothing(oExplorer) = True Then
            MessageBox.Show("Cannot open Outlook.")
            Exit Sub
        End If

        Dim selection As Selection = oExplorer.Selection
        If selection.Count = 0 Then
            Marshal.ReleaseComObject(selection)
            MessageBox.Show("Nothing selected.")
            Exit Sub
        End If

        Dim filename As String
        Dim ext As String = ".msg"

        Dim mail As MailItem                                ' Important, no 'Shadow'-objects, such as "For Each mail as MailItem in selection", cos you need to free it with Marshal.ReleaseComObject()...
        For Each mail In selection
            Dim subtxt As String = mail.Subject
            If Not String.IsNullOrEmpty(subtxt) Then
                If subtxt.Length > 120 Then
                    subtxt = Left(subtxt, 120)
                End If
            End If

            filename = fill_filename(mail.Attachments.Count.ToString, subtxt, mail.SenderName, mail.ReceivedTime.ToShortDateString)

            Dim newFile As String = IO.Path.Combine(fuldir, filename + ext)
            Dim count As Integer = 0
            While IO.File.Exists(newFile)
                count += 1
                If count > 25 Then
                    newFile = Nothing
                    Exit While
                End If

                newFile = IO.Path.Combine(fuldir, filename + "(" + count.ToString + ")" + ext)
            End While

            If String.IsNullOrEmpty(newFile) = False Then
                mail.SaveAs(newFile)
            End If
            oExplorer.RemoveFromSelection(mail) ' Important, to remove the object from Selection, ReleaseComObject() only don't do.
            Marshal.ReleaseComObject(mail)  
        Next

        Marshal.ReleaseComObject(selection)
    Catch ex As System.Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

此致

卡利

答案 2 :(得分:0)

使用委托并不是一个好主意,事实证明你可以采取一种解决方法来重新激活你的观点。

将mailitem保存到文件后,我调用了SwitchOutlookPanes()

 Public Sub SwitchOutlookPanes()
    Dim Outlook As Microsoft.Office.Interop.Outlook.Application
    Dim explorer As Microsoft.Office.Interop.Outlook.Explorer = Nothing
    Try
        If Outlook Is Nothing Then
            If Outlook Is Nothing Then Outlook = CType(Microsoft.VisualBasic.Interaction.GetObject("", "Outlook.Application"), Microsoft.Office.Interop.Outlook.Application)
            explorer = Outlook.ActiveExplorer
        End If

        If Outlook IsNot Nothing And explorer IsNot Nothing Then
            Dim nMAPIFOlder As Interop.Outlook.MAPIFolder = explorer.CurrentFolder
            explorer.CurrentFolder = Outlook.OlDefaultFolders.olFolderContacts
            System.Threading.Thread.Sleep(1500)
            explorer.CurrentFolder = nMAPIFOlder
        End If
    Catch ex As System.Exception
    Finally
        Marshal.ReleaseComObject(explorer)
        Marshal.ReleaseComObject(Outlook)
    End Try

End Sub

这会清除选择并阻止Outlook冻结/挂起..

此致

麦克