Cómo arrastrar (drag and drop) correos de Outlook a un DataGrid en VisualBasic VB

By | 12/01/2016

Código para arrastrar (drag and drop) correos de Outlook a un DataGrid en VisualBasic VB:

Hay que importar la libería: Microsoft.Office.Interop.Outlook

Hay que declarar la variable:

Dim objOL As New Microsoft.Office.Interop.Outlook.Application

Este sería el código para manejar el Mail arrastrado:

Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles DataGridView1.DragDrop
        Try
         Dim Ficheros_Subidos As Boolean = False
 
         Try
 
            DataGridView1.BackgroundColor = Color.FromKnownColor(KnownColor.ControlLight)
 
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then
               Dim Rutas As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
 
               Dim i As Integer = 0
 
               For Each archivo As String In Rutas
                 '''''''''''''OTROS FICHEROS

               Next
            ElseIf e.Data.GetDataPresent("FileGroupDescriptor") Then
				'CORREOS
				
               If Directory.Exists(PathDirectorioDocumentosDescargados) = False Then
                  Directory.CreateDirectory(PathDirectorioDocumentosDescargados)
               End If
 
               '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(PathDirectorioDocumentosDescargados, _
                                              (objMI.Subject + ".msg").Replace(":", ""))
 
                  objMI.SaveAs(strFile)
                  Dim Fichero As New FileInfo(strFile)
                  Fichero.CopyTo(PATH_ACTUAL & "\" & Fichero.Name, False)
               Next
            End If
 
         Catch ex As Exception
            MsgBox("ERROR DataGridView1_DragDrop:" & ex.Message)
         End Try
End Sub