Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. Office Outlook COM Interop best practice.

Office Outlook COM Interop best practice.

Scheduled Pinned Locked Moved COM
questioncsharpcomhelpwpf
1 Posts 1 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    The Fist
    wrote on last edited by
    #1

    Hello With the help of coffee and guesswork I've managed to use COM and Interop to open an Outlook .msg file and extract content from it. I have Outlook 2007 installed and I'm using Microsoft.Office.Interop.Outlook PIA runtime version 2.0.50727 (Version 14.0.0.0) I'm using .NET 4 in a WPF application. My problem is that my code starts an Outlook process but does not close it down afterwards. If I am already running Outlook, my code seems to attach to that process rather than start another one and if outlook is already running, my app.Quit() message closes the existing Outlook application (not the process.) My question is am I going about this in the wrong way? I've read lots of posts about using Outlook but nothing that describes with authority how I should cleanly open, interrogate and close an outlook message. Here's the code:

    public override void ViewContent(string strFilePath)
    {
    base.ViewContent(strFilePath);

    Microsoft.Office.Interop.Outlook.Application app = null;
    Microsoft.Office.Interop.Outlook.NameSpace session = null;
    Microsoft.Office.Interop.Outlook.MailItem item = null;

    try
    {
    // Start Outlook.
    app = new Microsoft.Office.Interop.Outlook.Application();
    // Do some voodoo.
    session = app.Session;
    // Get the MailItem we're interested in.
    item = session.OpenSharedItem(strFilePath) as Microsoft.Office.Interop.Outlook.MailItem;

    if(item != null)
    {
      // Set the Dependency properties that the UI has bound to.
      Subject = item.Subject;
      From = item.SenderName;
      if(string.IsNullOrEmpty(item.SenderEmailAddress) == false)
        From += "{" + item.SenderEmailAddress + "}";
    
      MessageText = item.Body;
    
      item.Unload += new ItemEvents\_10\_UnloadEventHandler(item\_Unload);
    
      item.Close(OlInspectorClose.olDiscard);
    }
    // This does not quit the Outlook process we started earlier.
    // If the full Outlook application is running, this line terminates that instead.
    // How can I quit the process I started earlier instead?
    app.Quit();
    

    }
    catch(Exception)
    {
    // Blah ...
    }
    finally
    {
    if(item != null)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(item);

    if(session != null)
      System.Runtime.InteropServices.Marshal.ReleaseComObject(session);
    
    if(app != null)
      System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
    
    item = null;
    session = null;
    app = null;
    

    }
    }

    Is this the right way

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups