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

Posted on Friday, March 20th, 2009 at 12:22 pm by Dmitry Kostochko

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

Post a comment

  • Sign In
  • SITEMAP