Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get body from Outlook email [Drag’n’Drop]

I’m working with WPF and I’m trying to make a drag’n’drop textbox.
In this textbox I want to get the body of an email which I drag from outlook.
The code works but I think I need something to “reset” the ActiveExplorer cause now it only shows the last “NEW” email which I drag into the textbox.

Example:

Drag email 1 -> Textbox - Shows email 1

Drag email 2 -> Textbox - Shows email 2

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


Hope my question is a bit clear to you..
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();
        }
    }
like image 779
jefsmi Avatar asked Oct 18 '11 12:10

jefsmi


2 Answers

I moved the declaration of oApp out of DragDrop event as 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 display only the last item because of this loop?

foreach (object item in oSelection)
{
    Outlook.MailItem mi = (Outlook.MailItem)item;
    myTextbox.Text = mi.Body.ToString(); //<--- Only last items text
}
like image 103
L.B Avatar answered Oct 26 '22 17:10

L.B


I updated L.B's answer. His DragEnter EventHandler automatically assumed that the user dropped in something from Outlook.

The result was that if the user dropped in something else (a file, selected text, ...), the code would still look at the currently selected emails in Outlook and ignore what was actually dropped.

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

There is a direct cast of the selected Outlook item to Outlook.MailItem. The code thus only works with actual emails. It is also possible to handle Outlook.MeetingItem, Outlook.ContactItem, Outlook.NoteItem and probably more.

like image 40
Laoujin Avatar answered Oct 26 '22 18:10

Laoujin