Solved

Drag Drop email from Outlook

Posted on 2010-08-26
9
1,156 Views
Last Modified: 2012-05-10
Hello,

I have the below code which works find in Outlook 2007 where I can drag an email from Outlook into the textbox and the message appears in the textbox but it doesn't seem to work in Office 2010. How can I fix this?

The error i am getting says "Cannot create ActiveX component" which is being caught in the FindOutlookItem function.

Any ideas would be appreciated.
Thanks in advance.
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports Microsoft.Office.Interop.Outlook '( Add com reference Microsoft Outlook 10.0 Object Library)
Imports System.Collections
Imports Microsoft.Office.Interop

Public Class frmDDTest

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
        If e.Data.GetDataPresent("RenPrivateMessages") Then
            e.Effect = DragDropEffects.All
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub


    Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
        Dim dropString As String = e.Data.GetData(DataFormats.UnicodeText).ToString
        Dim mailData As Hashtable = ParseOutlookDropData(dropString)
        TextBox1.Text = FindOutlookItem(mailData).Body
    End Sub

    Private Function ParseOutlookDropData(ByVal data As String) As Hashtable
        Dim infoLines As String() = data.Split(Microsoft.VisualBasic.Chr(10))
        Dim headers As String() = infoLines(0).Split(Microsoft.VisualBasic.Chr(9))
        Dim record As String() = infoLines(1).Split(Microsoft.VisualBasic.Chr(9))
        Dim mailData As Hashtable = New Hashtable
        Dim i As Integer = 0
        While i < headers.Length
            mailData.Add(headers(i), record(i))
            System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
        End While
        Return mailData
    End Function

    Private Function FindOutlookItem(ByVal mailData As Hashtable) As Outlook.MailItem
        Dim outlook As Outlook.Application

        Try
            outlook = CType(Microsoft.VisualBasic.Interaction.GetObject("", "Outlook.Application"), Outlook.Application)


            Dim explorer As Outlook.Explorer = outlook.ActiveExplorer
            Dim mail As Outlook.MailItem = CType(explorer.Selection.Item(1), Outlook.MailItem)

            Dim x As Integer = 1
            ' If mail.Subject = mailData("Betreff").ToString Then
            Return mail
            ' Else
            ' Throw New ApplicationException("Error -Mail Requested not found?")
            ' End If

        Catch ex As System.Exception

            MsgBox(ex.Message)
        End Try

    End Function
End Class

Open in new window

0
Comment
Question by:previewservices
  • 5
  • 4
9 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 33530111
you should go late-bound in not to be tied to any version of Outlook. check http://www.emoreau.com/Entries/Articles/2008/05/Dropping-a-Outlook-message-on-your-application.aspx
0
 

Author Comment

by:previewservices
ID: 33530256
Thanks emoreau but still getting the same error when trying to go late-bound. It errors on line "Dim objOL As Object = GetObject(, "Outlook.Application")"
System.InvalidOperationException was unhandled
  Message=An error occurred creating the form. See Exception.InnerException for details.  The error is: Cannot create ActiveX component.
  Source=DragDrop Test
  StackTrace:
       at DragDrop_Test.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
       at DragDrop_Test.My.MyProject.MyForms.get_Form1()
       at DragDrop_Test.My.MyApplication.OnCreateMainForm() in C:\Users\Ross.PREVIEW\AppData\Local\Temporary Projects\DragDrop Test\My Project\Application.Designer.vb:line 35
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at DragDrop_Test.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       Message=Cannot create ActiveX component.
       Source=Microsoft.VisualBasic
       StackTrace:
            at Microsoft.VisualBasic.Interaction.GetObject(String PathName, String Class)
            at DragDrop_Test.Form1..ctor() in C:\Users\Ross.PREVIEW\AppData\Local\Temporary Projects\DragDrop Test\Form1.vb:line 5
       InnerException:

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33530270
I will check tonight on another PC that has Outlook 2010 installed
0
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!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33536443
can you try my demo application? I just tried it at home with Outlook 2010 and it is working.
0
 

Author Comment

by:previewservices
ID: 33564941
Strange, I still get an error. Would it matter i'm using VS2010? I get the below error in the label.
An error occured in the drop event
System.InvalidCastException: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155).
   at Microsoft.Office.Interop.Outlook.ApplicationClass.ActiveExplorer()
   at WindowsApplication1.Form1.Label1_DragDrop(Object sender, DragEventArgs e) in C:\Users\Ross.PREVIEW\Desktop\Demo\Form1.vb:line 48

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33565536
I am using VS2010 too. Have you tried to cast as _Application?
0
 

Author Comment

by:previewservices
ID: 33565786
How do you mean? I'm not too familiar with this. Thanks for your help.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33566144
0
 

Author Comment

by:previewservices
ID: 33585156
I can't find the file msoutl.olb they talked about. I downloaded the demo of Office 2010 then entered the serial key. It seems to use a bootloader to run applications. For example the shortcut to Outlook is "C:\Program Files\Common Files\microsoft shared\Virtualization Handler\CVH.EXE" "Microsoft Outlook 2010 9014006204090000"
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

713 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