Solved

Drag and Drop Outlook Attachments to Windows Form vb.net

Posted on 2013-11-12
7
2,087 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39644573
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
ID: 39646163
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
ID: 39646254
Does RenPrivateItem have anything?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:BISCO3
ID: 39647990
No
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39648017
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
ID: 39648945
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
ID: 39649122
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

734 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