Get body from Outlook email [DragnDrop]

Im working with WPF and Im trying to create a dragndrop text box.
In this text box, I want to get the body of the letter that I drag from Outlook.
The code works, but I think I need something to "reset", the reason ActiveExplorer now shows only the last "NEW" letter, which I drag into the text box.

Example:

Drag Email 1 -> Text Box - Shows Email 1

Drag Email 2 -> Text Box - Shows Email 2

Drag email 1 -> Text box - Shows email address 2 and email address 1 will not be displayed because it already exists in ActiveExplorer and it will show email 2.


I hope you will understand my question a little.
Thanks in advance!

XAML Code:

<TextBox Name="myTextbox" AllowDrop="True" PreviewDragEnter="email_DragEnter" PreviewDrop="email_Drop" /> 

XAML code behind:

  private void email_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } private void email_Drop(object sender, DragEventArgs e) { Outlook.ApplicationClass oApp = new Outlook.ApplicationClass(); Outlook.Explorer oExplorer = oApp.ActiveExplorer(); Outlook.Selection oSelection = oExplorer.Selection; foreach (object item in oSelection) { Outlook.MailItem mi = (Outlook.MailItem)item; myTextbox.Text = mi.Body.ToString(); } } 
+8
c # outlook wpf drag-and-drop
source share
3 answers

I moved the oApp from the DragDrop event, as shown below, and it works as expected.

 void Startup() { _Outlook = new Outlook.Application(); } Outlook.Application _Outlook = null; private void Form1_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } private void Form1_DragDrop(object sender, DragEventArgs e) { richTextBox1.Text = ""; Outlook.Explorer oExplorer = _Outlook.ActiveExplorer(); Outlook.Selection oSelection = oExplorer.Selection; foreach (object item in oSelection) { Outlook.MailItem mi = (Outlook.MailItem)item; richTextBox1.AppendText(mi.Body.ToString() + "\n----------------------------------------\n"); } } 

-------- -------- EDIT

OR Is it possible that you will only display the last item due to this loop?

 foreach (object item in oSelection) { Outlook.MailItem mi = (Outlook.MailItem)item; myTextbox.Text = mi.Body.ToString(); //<--- Only last items text } 
+9
source share

I updated LB's answer. His DragEnter EventHandler automatically assumed that the user was missing something from Outlook.

As a result, if the user drops something else (file, selected text, ...), the code will still look at the currently selected emails in Outlook and ignore what was actually deleted.

The code:

 Private _Outlook As Outlook.Application = Nothing Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load _Outlook = New Outlook.Application() End Sub Private Sub Form_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragEnter Dim outlookRequiredFormats = New String() { _ "RenPrivateSourceFolder", _ "RenPrivateMessages", _ "RenPrivateItem", _ "FileGroupDescriptor", _ "FileGroupDescriptorW", _ "FileContents", _ "Object Descriptor"} If outlookRequiredFormats.All(Function(requiredFormat) e.Data.GetDataPresent(requiredFormat)) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub Private Sub Form_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragDrop Dim oExplorer As Outlook.Explorer = _Outlook.ActiveExplorer() Dim oSelection As Outlook.Selection = oExplorer.Selection Dim i As Integer = 0 For Each item As Object In oSelection Dim mi As Outlook.MailItem = DirectCast(item, Outlook.MailItem) mi.SaveAs("C:\YourPath\message" & i & ".msg") i += 1 Next 

Direct cast of the selected Outlook item to Outlook.MailItem . Thus, the code only works with actual email messages. It is also possible to handle Outlook.MeetingItem , Outlook.ContactItem , Outlook.NoteItem and possibly more.

+1
source share

Using version 14.0.0.0 for Microsoft.Office.Interop.Outlook.dll I cannot use the Outlook.ApplicationClass object.

Instead, I used Outlook.Application in the example you gave, and it works like a charm (tested with windows seven and Outlook 2007 SP2). I can drag and drop letters as I see fit.


PS: MSDN Extract for ApplicationClass :

"This class supports the .NET Framework and is not intended to be used directly from your code."

0
source share

All Articles