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