4

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

例如:我拖放邮件 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 属性可以看出,它根本不清楚

4

3 回答 3

6

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

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

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

e.data.GetData("RenPrivateMessages");

于 2015-08-24T08:36:25.640 回答
1

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

最后的线索是使用活动资源管理器的 RemoveFromSelection 方法,因为 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

问候,

卡利

于 2013-08-28T09:25:10.827 回答
0

使用委托并不是一个好主意,事实证明你可以做一个解决方法来重新激活你的前景。

将邮件项保存到文件后,我调用 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 冻结/挂起。

问候,

麦克风

于 2013-01-25T19:22:26.697 回答