Skip to content

Visual Studio 2008 c# app to drag email to a form.. not quite working.

markm75c

Asked May 7, 2008, 12:41 PM EDT1 answer
Share
LinkedInX

I have this windows form app.. where i have a text box and from outlook i want to be able to drag and drop from outlook to the form and basically copy to this text box the subject and body text...

I thought i almost had this figured out.. here is the code near the end that tries to "find" the outlook (2007) window and find the mail item data:

Java
  Microsoft.Office.Interop.Outlook.Application outlook;
                //outlook = (Microsoft.VisualBasic.Interaction.GetObject("", "Outlook.Application")) as Microsoft.Office.Interop.Outlook.Application;
                try
                {

                    //try to create comm component with vb interaction
                    outlook = (Microsoft.Office.Interop.Outlook.Application)Microsoft.VisualBasic.Interaction.GetObject("", "Outlook.Application");
                }
                catch (System.Exception ex)
                {
                    throw new ApplicationException("error" + ex);
                }

                //get the active one
                Microsoft.Office.Interop.Outlook.Explorer explorer = outlook.ActiveExplorer();
                Microsoft.Office.Interop.Outlook.MailItem mail = (Microsoft.Office.Interop.Outlook.MailItem)explorer.Selection[0];


                //if (mail.Subject = maildata("Betreff").ToString())
                //{
                return mail;
It gets stuck on the line
                    outlook = (Microsoft.Office.Interop.Outlook.Application)Microsoft.VisualBasic.Interaction.GetObject("", "Outlook.Application");


It will either sit there forever or if i actually put a break on the catch, i am shown "cannot create activex component" as an error...

I'm not sure how else to go about this.. any thoughts?

Thanks

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Hi,

    This is how I did it

    First you need to make the AllowDrop = true;

    then I use the following: for dragging and droping from outlook

    Thanks John

    TypeScript
    private void _DragEnter(object sender, DragEventArgs e)
    {
    e.Effect = DragDropEffects.All;
    }
    private void _DragDrop(object sender, DragEventArgs e)
    {
    
          Outlook.ApplicationClass app = new Outlook.ApplicationClass();
          Outlook.Selection sel = app.ActiveExplorer().Selection;
    foreach (object mitem in sel)
    {
    try
    {
    Outlook.ContactItem ci = (Outlook.ContactItem)mitem;
    ci.SaveAs(Path.GetTempPath() + ci.FirstName + ".vcf", Outlook.OlSaveAsType.olVCard);
    
    }
    catch
    {
    try
    {
    Outlook.MailItem mi = (Outlook.MailItem)mitem;
    mi.SaveAs(Path.GetTempPath() + mi.Subject.ToString() + ".msg", Outlook.OlSaveAsType.olMSG);
    }
    catch
    { }
    }
    }
    }
    0