VB.net 将 SINGLE Outlook 消息拖放到表单上

VB.net Drag SINGLE Outlook msg and drop on form

我有代码可以将 Outlook 项目拖放到我的表单(标签)上,但我想确保只拖放一个文件。那么我该如何正确编码以禁止来自 outlook 的多个文件并提示用户只输入一个文件?

Private Sub Label1_DragDrop(sender As Object, e As DragEventArgs) Handles Label1.DragDrop
        lblFile.Text = String.Empty
        Try

            If e.Data.GetDataPresent("FileGroupDescriptor") Then
                'supports a drop of a Outlook message

                'Dim objMI As Object - if you want to do late-binding
                Dim objMI As Microsoft.Office.Interop.Outlook.MailItem

                For Each objMI In objOL.ActiveExplorer.Selection()
                    'hardcode a destination path for testing
                    Dim strFile As String = _
                                IO.Path.Combine("c:\temp", _
                                                (objMI.Subject + ".msg").Replace(":", ""))
                    lblFile.Text += strFile + Environment.NewLine
                    objMI.SaveAs(strFile)
                Next
            End If
            lblFormat.Text = String.Empty
            IO.File.Delete(lblFile.Text)
        Catch ex As Exception
            lblFile.Text = "An error occured in the drop event" + Environment.NewLine + ex.ToString
        End Try
    End Sub

根据文档,Application.ActiveExplorer method returns an Explorer object. The Selection() property of the Explorer object returns a Collection 对象。

A Collection 是一个可枚举对象,因此它有一个名为 .Count 的 属性,它会告诉您它包含多少项。您可以使用此 属性 检查一个选择是否包含多个项目,如下所示:

if objoL.ActiveExplorer.Selection.Count >1 then