I'm trying to import an outlook message to my vb.net form to populate textboxes/richtextboxes. I've used some code from Eric Moreau which handles the import function. The problem is that the code imports the message and saves it to a temporary folder. My issue here is that I would need a solution without any kind of saving. Instead it should populate a richtextbox field and then I will use that richtextbox to save it to my.settings of the application. I can't seem to figure out what to change in order to change the behaviour from saving to actually populating a field of mine. The code is below (All cred to Eric Moreau for the original code)
Option Strict On
Public Class MailDnD
Dim objOL As New Microsoft.Office.Interop.Outlook.Application
Private Sub me_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
lblFile.Text = String.Empty
Try
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
'supports a drop of a file from Windows Explorer
' copy the name of the dragged files into a string array
Dim draggedFiles As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
'handle each file passed as needed
For Each fileName As String In draggedFiles
'hardcode a destination path for testing
Dim strDestinationFile As String = _
IO.Path.Combine(My.Settings.TempFolder.ToString, _
IO.Path.GetFileName(fileName))
'test if source and destination are the same
If strDestinationFile.Trim.ToUpper = fileName.Trim.ToUpper Then
lblFile.Text += strDestinationFile + _
" - E-post meddelandet är redan importerat!" + _
Environment.NewLine
Else
lblFile.Text += "Importerar - " + _
strDestinationFile + Environment.NewLine
IO.File.Copy(fileName, strDestinationFile)
End If
Next
ElseIf 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(My.Settings.TempFolder.ToString, _
(objMI.Subject + ".msg").Replace(":", ""))
lblFile.Text += strFile + Environment.NewLine
objMI.SaveAs(strFile)
Next
End If
lblFormat.Text = String.Empty
Catch ex As Exception
lblFile.Text = "Ett fel uppstod vid import, vänligen testa igen" + Environment.NewLine + ex.ToString
End Try
End Sub
''' <summary>
''' Reset the status label
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub me_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DragLeave
lblFormat.Text = String.Empty
End Sub
''' <summary>
''' Handle the DragOver event
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub me_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
'handle a file dragged from Windows explorer
e.Effect = DragDropEffects.Copy
lblFormat.Text = "Dra över e-post meddelandet"
ElseIf e.Data.GetDataPresent("FileGroupDescriptor") Then
'handle a message dragged from Outllok
e.Effect = DragDropEffects.Copy
lblFormat.Text = "Dra över e-post meddelandet"
Else
'otherwise, do not handle
e.Effect = DragDropEffects.None
lblFormat.Text = ""
End If
End Sub
Just to clarify the import function works as intended. It saves the outlook message to the folder but I kinda want it to not save and instead import the message line to line to a richtextbox inside my application. Hook me up if you need more information
Kind regards,