Search code examples
c#.netoffice-interopoutlook-2003

OpenSharedItem with Outlook interop throws odd exception with Office 2003, works with Office 2008


I'm using the Office interop API to open a .msg file saved from outlook, and to then show a reply window to allow the user to reply to it.

When running Office 2003, the OpenSharedItem(pathToMSGFile); call throws the following exception:

Unhandled Exception: System.AccessViolationException: Attempted to read or write
  protected memory. This is often an indication that other memory is corrupt.
  at Microsoft.Office.Interop.Outlook._NameSpace.OpenSharedItem(String Path)
  at OutlookTest.Program.Main(String[] args)

When running Office 2008, it works absolutely fine.

I've put together a small test case, the code is as follows:

static void Main(string[] args)
{
    try
    {
        Application app;
        string pathToMSGFile = "\\\\path\\to\\foobar.msg";

        if (args.Length > 0)
        {
            pathToMSGFile = args[0];
        }

        if (!File.Exists(pathToMSGFile))
        {
            Console.WriteLine("{0} does not exist.", pathToMSGFile);
            return;
        }

        Console.WriteLine("Opening {0}", pathToMSGFile);

        Type olType = Type.GetTypeFromProgID("Outlook.Application", false);

        app = Activator.CreateInstance(olType) as Application;

        MailItem fld = (MailItem)app.Session.OpenSharedItem(pathToMSGFile);

        _MailItem reply = fld.ReplyAll();
        reply.Save();
        reply.Display(false);

        Console.ReadKey();

        reply.Close(OlInspectorClose.olDiscard);
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.ToString());

        if (ex.InnerException != null)
        {
            Console.WriteLine(ex.InnerException.ToString());
        }
    }

    Console.ReadKey();
}

The application is targeted as .Net 4, using the Office12 interop library. Same happens regardless of whether its compiled for AnyCPU or x86.


Solution

  • I note that version 11 of the interop library does not contain an OpenSharedItem() method.

    It becomes available from version 12 onwards.

    It appears that this operation is unavailable on Office 11/2003 (at least not via that method call on any version of the interop lib).


    I'm not sure if this is suitable for your scenario, but I've had good success with the Outlook Redemption library.

    From What is Outlook Redemption?:

    Outlook Redemption works around limitations imposed by the Outlook Security Patch plus provides a number of objects and functions to work with properties and functionality not exposed through the Outlook object model.

    Redemption library also includes the RDO (Redemption Data Objects) family of objects that can function as a complete CDO 1.21 or Outlook Object Model replacement.

    It seems to circumvent some of these odd/inconsistent behaviours b/w different versions of Outlook (either by design or as a "side effect" of the original goal).

    If you're otherwise familiar with CDO, you'll be comfortable with RDO. But to be honest I don't know how it "maps" to Microsoft.Office.Interop.Outlook.

    Refer http://www.dimastr.com/redemption/rdo/rdosession.htm

    The equivalent RDO function for Session.OpenSharedItem() is RDOSession.GetMessageFromMsgFile().

    NB I'm in no way affiliated with this product, other than I've used it on occasion! :-)