Drag Outlook Attachment To Your WinForm

My applications usually allow users to drag and drop Outlook attachments and save these as files. I have a simple method which saves the memory stream from Outlook to a temporary file and returns the path to the file.

Here is the function

Public Function GetOutlookAttachment(ByVal e As System.Windows.Forms.DragEventArgs) As String
Dim theStream As IO.Stream = DirectCast(e.Data.GetData("FileGroupDescriptor"), IO.Stream)
Dim fileGroupDescriptor As Byte() = New Byte(511) {}
theStream.Read(fileGroupDescriptor, 0, 512)

' used to build the filename from the FileGroupDescriptor block
Dim fileName As New System.Text.StringBuilder("")

' this trick gets the filename of the passed attached file
Dim i As Integer = 76
While fileGroupDescriptor(i) <> 0
fileName.Append(Convert.ToChar(fileGroupDescriptor(i)))
i += 1
End While
theStream.Close()
Dim path As String = IO.Path.GetTempPath()

' put the zip file into the temp directory
Dim theFile As String = path + fileName.ToString()
TempFiles.Add(theFile)

Dim ms As IO.MemoryStream = DirectCast(e.Data.GetData("FileContents", True), IO.MemoryStream)

' allocate enough bytes to hold the raw data
Dim fileBytes As Byte() = New Byte(ms.Length - 1) {}

' set starting position at first byte and read in the raw data
ms.Position = 0
ms.Read(fileBytes, 0, CInt(ms.Length))

' create a file and save the raw zip file to it
Dim fs As New IO.FileStream(theFile, IO.FileMode.Create)
fs.Write(fileBytes, 0, CInt(fileBytes.Length))

fs.Close()
' close the file

Return theFile
End Function

This function can be called like this

Private Sub MyForm_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop

If e.Data.GetFormats(False)(1) = "RenPrivateFileAttachments" Then

Dim FilePath as String = GetOutlookAttachment(e)

End If

End Sub

Advertisements

Tags: , , , , , , , , ,

7 Responses to “Drag Outlook Attachment To Your WinForm”

  1. Kay Says:

    Hi,
    I was wondering if you knew how to create a numericupdown in a datagridview in vb.net??
    Thanks

  2. codecruiser Says:

    Try following this tutorial

    http://msdn.microsoft.com/en-us/library/aa730881(v=vs.80).aspx

  3. gareth Says:

    Thanks SO much, very helpful to this .NET newbie. The only change I had to make was :
    Dim fileBytes As Byte() = New Byte(CInt(ms.Length – 1)) {} ‘ I had to add the CINT

  4. mark Says:

    isnt it amazing when stuff just works. thanks for this, you saved me a week of heartache

  5. Carl Says:

    This is great…how would I capture the the msg file if I dragged that over as well?

  6. codecruiser Says:

    @Carl Check http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C

  7. Shamalamalan Says:

    Old code, still works! Removed the Tempfiles.add(thefile) line, I’m not sure what that was for as tempfiles was never defined anywhere. Saved me a ton of heartache, +1 to you sir.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: