Dmitry Kostochko

HowTo: Get properties of an Outlook email item drag-and-dropped onto a .NET form

There is only one easy and reliable way to get a valid Outlook object when dragging Outlook items onto a .NET form of your Outlook add-in. Let’s have a good look at this way by example of MailItem and an advanced Outlook form region.

So, we will need an instance of the advanced form region with TextBoxes in which we are going to display some MailItem properties. Set the AllowDrop property to true, and add two event handlers – DragOver and DragDrop.

In the DragOver event handler, check the Selection collection and make sure it contains a MailItem object (the code below is in VB.NET, a C# sample is available for download at the end of the post):

Private Sub ADXOlForm1_DragOver(ByVal sender As System.Object, _
  ByVal e As System.Windows.Forms.DragEventArgs) _
  Handles MyBase.DragOver
    e.Effect = DragDropEffects.None
    Dim selection As Outlook.Selection = TryCast(ExplorerObj, _
      Outlook._Explorer).Selection
    If selection IsNot Nothing Then
        Try
            If selection.Count = 1 Then
                Dim mailItem As Outlook._MailItem = _
                  TryCast(selection.Item(1), Outlook._MailItem)
                If mailItem IsNot Nothing Then
                    Try
                        e.Effect = DragDropEffects.All
                    Finally
                        Marshal.ReleaseComObject(mailItem)
                    End Try
                End If
            End If
        Finally
            Marshal.ReleaseComObject(selection)
        End Try
    End If
End Sub

In the DragDrop event handler, get the MailItem object from the Selection collection and fill in the TextBoxes with appropriate Outlook MailItem properties:

Private Sub ADXOlForm1_DragDrop(ByVal sender As System.Object, _
  ByVal e As System.Windows.Forms.DragEventArgs) _
  Handles MyBase.DragDrop
    Dim selection As Outlook.Selection = TryCast(ExplorerObj, _
      Outlook._Explorer).Selection
    If selection IsNot Nothing Then
        Try
            Dim mailItem As Outlook._MailItem = _
              TryCast(selection.Item(1), Outlook._MailItem)
            If mailItem IsNot Nothing Then
                Try
                    textBoxTo.Text = GetRecipients(mailItem, _
                      Outlook.OlMailRecipientType.olTo)
                    textBoxCC.Text = GetRecipients(mailItem, _
                      Outlook.OlMailRecipientType.olCC)
                    textBoxSubject.Text = mailItem.Subject
                    textBoxBody.Text = mailItem.Body
                Finally
                    Marshal.ReleaseComObject(mailItem)
                End Try
            End If
        Finally
            Marshal.ReleaseComObject(selection)
        End Try
    End If
End Sub

That’s all with it. Have a good weekend!

You may also be interested in:

Advanced Outlook regions: Reading pane, Outlook bar, To-Do bar
Outlook plug-in development: toolbars, ribbons

Available downloads:

These sample add-ins were written on Add-in Express for Office and .net

C# sample Outlook add-in for VS 2005  
VB.NET sample Outlook add-in for VS 2005

6 Comments

  • https://secure.gravatar.com/avatar/1fe1043c872297ac99a26042c877fcc4?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dan says:

    Is it possible to do a simialr thing with mail objects that are dropped onto Outllok folders?
    I have tried using the ClipboardContents from the ExplorerBeforeItemPasts event args but cannot seem to be able to cast this as a Outllok mail item.

    Thanks,
    Dan

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi Dan,

    Yes, it is possible. You can use the ItemAdd event of the Folder.Items collection. Or, if you need to intercept just moving emails between Outlook folders, you can use the Folder.BeforeItemMove event, this event provides parameters to identify which exactly item is being moved and to which destination folder.

  • https://secure.gravatar.com/avatar/c88b62ae451a1601feb5243837576ef5?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Eric Legault says:

    Hi Dmitry! So I assume there is no way to use an IDataObject to get an Outlook.MailItem object (or a collection of)?

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi Eric,

    As far as I know there is no such way. IDataObject may contain simple types like strings, bitmap/metafile handles and simple COM interfaces like IStream and IStorage.

  • https://secure.gravatar.com/avatar/8f16f6777224b8b86a3553f321c4feee?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Brain2000 says:

    Here is some code on how to convert an IDataObject to an Outlook MSG:

    http://www.codeproject.com/Articles/32899/Reading-an-Outlook-MSG-File-in-C

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi Brain,

    Thank you for this solution.

Post a comment

Have any questions? Ask us right now!