Solved

Drag and Drop Outlook Attachments to Windows Form vb.net

Posted on 2013-11-12
7
1,916 Views
Last Modified: 2013-12-09
I have a Windows Form that I enabled Drag and Drop functionality.

Here's my question:
We use Outlook 2007 and I was wondering how to extract the contents of a selected attachment from an email?

I have no problems extracting Word, Excel, Pdf or other document types.

I use the following code:
        If e.Data.GetDataPresent("FileGroupDescriptor") Then
            Try
                Cursor.Current = Cursors.WaitCursor
                Refresh()
                Dim ioStream As IO.Stream = DirectCast(e.Data.GetData("FileGroupDescriptor"), IO.Stream)

                Dim FileGroupDescriptor(CInt(ioStream.Length)) As Byte
                ioStream.Read(FileGroupDescriptor, 0, FileGroupDescriptor.Length) 'Build the FileName from the FileGroupDescriptor block.
                'The first position of the FileGroupDescriptor indicates how many attachments are being dragged into the FileName Textbox.
                If FileGroupDescriptor.Length > 0 AndAlso CInt(FileGroupDescriptor(0)) > 1 Then
                    MessageBox.Show("Only 1 file may be dragged and dropped onto the Scan Form.", "Too Many Files", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    ioStream.Close()
                    SetInputFocus()
                    Exit Sub
                End If
                Dim AttachmentFileName As New System.Text.StringBuilder("")
                'Get the filename of the attachment.
                Dim i As Integer = 76 'The FileName starts at position 76 in the FileGroupDescriptor.  Thus, start at position 76.  While Loop will terminate once the end of the FileGroupDescriptor is reached.
                While FileGroupDescriptor(i) <> 0
                    AttachmentFileName.Append(Convert.ToChar(FileGroupDescriptor(i)))
                    i += 1
                End While
                ioStream.Close()

                Dim TempDirectory As New DirectoryInfo("C:\bsco\" & BiscoActions.USER)
                If Not TempDirectory.Exists Then TempDirectory.Create()

                Dim FileName As String = "C:\bsco\" & BiscoActions.USER & "\" & AttachmentFileName.ToString()
                Dim FileExtension As String = Path.GetExtension(FileName).Substring(Path.GetExtension(FileName).IndexOf(".") + 1).ToUpper
                If FileExtension = "EXE" OrElse FileExtension = "BAT" OrElse FileExtension = "LNK" OrElse FileExtension = "JS" OrElse FileExtension = "ICO" OrElse FileExtension = "ZIP" OrElse FileExtension = "DLL" OrElse FileExtension = "" Then
                    Select Case FileExtension
                        Case ""
                            MessageBox.Show("Folders and Files without a valid file extension cannot be added to the database.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                        Case Else
                            MessageBox.Show(FileExtension & " is an invalid file type.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    End Select
                    SetInputFocus()
                    Exit Sub
                End If

                       Dim ms As System.IO.MemoryStream = DirectCast(e.Data.GetData("FileContents", True), IO.MemoryStream)
                        Dim FileBytes As Byte() = New Byte(CInt(ms.Length - 1)) {}
                        ms.Read(FileBytes, 0, CInt(ms.Length))

                        'Create a file in the Temp Directory.
                        Dim fsAttachment As New IO.FileStream(FileName, IO.FileMode.Create, FileAccess.Write)
                        fsAttachment.Write(FileBytes, 0, CInt(FileBytes.Length))

                        ms.Close()
                        fsAttachment.Close()


My issue is if an email, which is an attachment to another email, is selected and dragged to my Form, the above code fails because the FileContents is Nothing.

Any suggestions on how I could extract the contents of an attachment which happens to be another email?
0
Comment
Question by:BISCO3
  • 4
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
My first unknown is the different formats that are available to you when you drop the message on your form...
0
 

Author Comment

by:BISCO3
Comment Utility
when adding an attachment, the following formats are available to you:

FileGroupDescriptorW
FileGroupDescriptor
RenPrivateItem
FileContents

Yet, an MSG File that is an attachment, has nothing in its FileContents
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Does RenPrivateItem have anything?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:BISCO3
Comment Utility
No
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
I don't see any other format that would help, so it looks like you would need to go in a different direction from drag and drop operations, like the interop COM library, or Redemption.  I don't have specifics, off the top of my head, for that kind of operation, but I could look around and see what I have in my bag-o-tricks.
0
 

Author Comment

by:BISCO3
Comment Utility
RenPrivateItem does not work.  The follow code results in the Catch, of the Try-Catch, to fire:
"Dim ms As System.IO.MemoryStream = DirectCast(e.Data.GetData("RenPrivateItem", True), IO.MemoryStream)"

Both, FileGroupDescriptorW and FileGroupDescriptor, result in the file being copied and saved, yet trying to reopen the file has been less than successful.  Most often, a Microsoft Outlook error pops up and says that the file may not exist, that the user doesn't have the right permissions for it, or that it could be open in another program.  When double clicking email message (.MSG file) that was copied to a directory on my computer, the same error occurs.

Somewhere in the code below, the message is not being copied properly.
(This is the same code as the initial message)

Dim ms As System.IO.MemoryStream = DirectCast(e.Data.GetData("FileContents", True), IO.MemoryStream)
Dim FileBytes As Byte() = New Byte(CInt(ms.Length - 1)) {}
ms.Read(FileBytes, 0, CInt(ms.Length))

'Create a file in the Temp Directory.
Dim fsAttachment As New IO.FileStream(FileName, IO.FileMode.Create, FileAccess.Write)
fsAttachment.Write(FileBytes, 0, CInt(FileBytes.Length))
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
That sounds like a different problem.

Is this the message that you are describing?

Err Msg: Access is Denied. You Don't Have Permissions or the File is in Use
http://support.microsoft.com/kb/245068
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now