1

I've got a user who wants to be able to drag an attachment from Outlook to a vb.net Winforms application. How do I do this?

I found this link: https://www.codeproject.com/Articles/7140/Drag-and-Drop-Attached-File-From-Outlook-97-and-ab, but some of the examples seem to be missing plus it's in C#.

I'm also concerned since we're using a legacy app written in Visual Studio 2012 with .net 4.6.1 (this can't be changed to do licensing and such).

Can someone give me someplace to get started?

** Update - 2/21/22 **

I put together code based on the website referenced above and the sample code. The sample code did not work, but it helped me fill in the blanks from the website. I now have the issue that dragging an attachment to the Winform gives me ALL the attachments. The code takes the e-mail, loops through all the attachments, and processes them all. Is there anyway to get just the attachment that is dragged over?

Here is the code on the form DragDrop event:

  Private Sub frmPurchaseOrder_DragDrop(sender As Object, e As DragEventArgs) Handles MyBase.DragDrop
        lstFiles.ClearFiles()
        Try
            If e.Data.GetDataPresent("FileGroupDescriptor") Then
                Dim explorer As Explorer = mobjApplication.ActiveExplorer()
                If explorer.AttachmentSelection.Count > 0 Then
                    Dim i As Integer = 0
                    For Each objAttachment As Attachment In explorer.AttachmentSelection
                        If objAttachment IsNot Nothing Then
                            lstFiles.AddFile("attachment #" + i.ToString())
                            lstFiles.AddFile("File Name: " + objAttachment.FileName)
                            Dim strFile As String = Path.Combine("c:\temp", FixFileName(objAttachment.FileName))
                            objAttachment.SaveAsFile(strFile)
                            Marshal.ReleaseComObject(objAttachment)
                            i += 1
                        End If
                    Next
                Else
                    'supports a drop of a Outlook message
                    For Each objMi As MailItem In mobjApplication.ActiveExplorer.Selection()
                        'hardcode a destination path for testing
                        Dim strFile As String = Path.Combine("c:\temp", FixFileName(objMi.Subject + ".msg"))
                        'lstFiles.AddFile(strFile)
                        objMi.SaveAs(strFile)
                        GetAttachmentsInfo(objMi)
                    Next
                End If
            End If

        Catch ex As System.Exception
            'lstFiles.Items.Add("An error occured in the drop event")
            'lstFiles.Items.Add(ex.ToString)
            MessageBox.Show("Error adding file: " & ex.ToString, "Error adding file")
        End Try
    End Sub
boilers222
  • 1,901
  • 7
  • 33
  • 71
  • Are you running into a particular problem? – Dmitry Streblechenko Feb 07 '22 at 22:13
  • [Drag-and-Drop multiple Attached File From Outlook to C# Window Form](https://stackoverflow.com/q/8709076/7444103) -- Note that it comes from here: [Outlook Drag and Drop in C#](https://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C) and assumes a 32-bit installation. There's a (contrived / partially wrong) fix in another answer. – Jimi Feb 08 '22 at 02:09
  • @DmitryStreblechenko "...running into a particular problem?" Yes. The examples for Steps 1-4 seem to be missing when I pull up the site. Also, this is for C# and I need a vb.net approach specifically one I can implement in an older version on VS 2012. – boilers222 Feb 09 '22 at 15:01

1 Answers1

0

Nothing really has been changed so far. You are referring to the valid article with sample code which can be used nowadays. Take a look at the https://github.com/tonyfederer/OutlookFileDrag sample code.

If you need to get VB.NET code instead of C# there are multiple converters on the web. But I'd suggest converting the code manually.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45