Drag Drop Outlook MailItem onto ListView

Hi.

I am trying to dragdrop a outlook email item onto a listview, then save the item to disk as a .msg file but am unable to do so. I am using VS .NET 2003 Academic version, Windows XP.

The following is the code I am using. The filename is recieved, but I am unable to read the FileContents section (As it becomes nothing when I convert it to a stream). Also, the e.Data item will not cast to a Outlook.MailItem item. <Confusion, Anger, Sobbing, Wailing!!!> I don't care how I do this as long as the result is a .msg file on disk, containing the outlook mailitem. I can then pass the filepath to another function to complete my ultimate goal (hahaha!) !

Any help on this would be appreciated.
Sad
Thanks,
Nick.

PrivateSub lvEventObjects_DragEnter(ByVal senderAsObject,ByVal eAs System.Windows.Forms.DragEventArgs)Handles lvEventObjects.DragEnter

'Purpose: Changes the cursor to represent an allowed move if the
' item is a valid listview item. This is so that an evaluators
' details can be dragged from the evaluators list form onto
' the evaluation form.

'See if the item being dragged is a listview item
e.Effect = DragDropEffects.Copy

EndSub

PrivateSub lvEventObjects_DragDrop(ByVal senderAsObject,ByVal eAs System.Windows.Forms.DragEventArgs)Handles lvEventObjects.DragDrop

If e.Data.GetDataPresent("FileGroupDescriptor") =TrueThen

'if the file is an email

Try

CallMe.GetFileDescriptor(e)

Dim iDOAsNew DataObject
iDO.SetData("System.IO.MemoryStream",
True, e.Data.GetData("System.IO.MemoryStream",True))

Dim io_msAs Stream =CType(e.Data.GetData("FileContents",True), Stream)

Dim fContentsAs Array = Array.CreateInstance(GetType(Byte), 512)
Call fContents.Clear(fContents, 0, 512)

io_ms.Read(fContents, 0, 512)

' Gets the data format(s) in the data object.
Dim arrayOfFormatsAs [String]() = iDO.GetFormats()

' Stores the results in a string.
Dim theResultAsString = "The format(s) associated with the data are:" + _ ControlChars.Cr

Dim iAsInteger

For i = 0To arrayOfFormats.Length - 1
theResult += arrayOfFormats(i) + ControlChars.Cr
Next i

' Show the results in a message box.

MessageBox.Show(theResult)

Dim
outlook_objAs Outlook.Application
outlook_obj = CreateObject("Outlook.Application")

Dim outlook_mailitemAs Outlook.MailItem
' outlook_mailitem = CreateObject("Outlook.MailItem")
'outlook_mailitem = iDO.GetData("FileContents", True)

Dim dofAs DataFormats.Format

Console.WriteLine(dof.Name)

' Console.WriteLine(DataFormats.Format)

'obj = iDO.GetData(DataFormats.GetFormat("FileContents", True)

'Now save it to the database

Catch exAs Exception
MsgBox("Exception: " & ex.Message)
EndTry

EndIf

'Update the list of event objects for the current item

' Call PopulateEventObjectList()

EndSub

FriendStructure SizeL

Friend WidthAsLong
Friend HeightAsLong

EndStructure

FriendStructure PointL

Friend xAsLong
Friend yAsLong

EndStructure

FriendStructure FILESIZE

Friend highAsDouble
Friend lowAsDouble

EndStructure

FriendStructure FileDescriptor

'Describes a file.
Friend dwFlagsAsDouble
Friend CLSIDAs Guid
Friend SiZELAs SiZEL
Friend PointLAs PointL
Friend dwFileAttributesAsDouble
Friend ftCreationTimeAs System.Runtime.InteropServices.FILETIME
Friend ftLastAccessTimeAs System.Runtime.InteropServices.FILETIME
Friend ftLastWriteTimeAs System.Runtime.InteropServices.FILETIME
Friend nFileSizeHighAs FILESIZE
Friend FileNameAsString

EndStructure

FriendFunction GetFileDescriptor(ByRef eAs System.Windows.Forms.DragEventArgs)As FileDescriptor

'get the file group descriptor data

Try

' Dim theStream As System.IO.Stream = CType(e.Data.GetData("FileGroupDescriptor"), Stream)

Call WriteFileFormats(e)
Dim idoAs DataObject = e.Data
Dim theblobAsByte() = ido.GetData(GetType(Byte))
Dim theStreamAs System.IO.Stream =CType(e.Data.GetData("FileContents"), Stream)
Console.WriteLine(theStream.Length())

' Dim theStream As System.IO.Stream = CType(e.Data.GetData("Outlook.MailItem", True), Outlook.MailItem)
'Dim mi As Outlook.MailItem
' mi = CreateObject("Outlook.MailItem")
' mi = CType(e.Data.GetData("Outlook.MailItem"), Outlook.MailItem)

' Console.WriteLine(mi.SenderEmailAddress.ToString)
' mi.SaveAs("C:\mymail.msg")

Dim fileGroupDescriptorAs Array = Array.CreateInstance(GetType(Byte), 512)
Call fileGroupDescriptor.Clear(fileGroupDescriptor, 0, 512)
theStream.Read(fileGroupDescriptor, 0, 512)

'write the output to a file
Call WriteByteArrayToFile_str(fileGroupDescriptor, 0, 512, "C:\mytest.txt")
Dim fdFileDescriptorAsNew FileDescriptor
theStream.Position = 0

'Get the dwflags (Double)
theStream.Close()

Catch exAs Exception
MsgBox(ex.Message)
EndTry

EndFunction

[15865 byte] By [NLARGE] at [2008-2-8]
# 1

From what I have been able to research there is not a good way to do this. The data you get back can vary depending on the type of object and the view. Here is a possible hack. It needs some work, but gives you an idea of what might be possible. I am assuming that this is a seperate application. It would be even easier if it were an outlook addin.

Add a refernce to the Microsoft Outlook Object Model

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

Dim app As New Microsoft.Office.Interop.Outlook.Application()

DirectCast(app.ActiveExplorer.Selection(1), Microsoft.Office.Interop.Outlook.MailItem).SaveAs("C:\TEST.MSG", Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG)

End Sub

Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter

e.Effect = DragDropEffects.Copy

End Sub

Sorry I couldn't do better

Regards,
Patrick Baker
Microsoft Visual Basic Deployment & Designer Team


This posting is provided "AS IS" with no warranties, and confers no rights.

PatrickBaker at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...