Question

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.

Was it helpful?

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! :-)

OTHER TIPS

This problem occurs when you try to save a message that contains a large number of attachments. When you find a message in Outlook, the menu file -> saveas also end up with the same error.

Reading E-mails From Outlook 2003 office is Possible But Not from the Path ( to .msg file) but we can read the E-mails Messages within the Outlook Folder( default / Other Folders ) as Mail-Items object.

If its coded in COM object 12.0 outlook object library and used in office 2003 (which has outlook 11.0 object lib) obviously it won't work !

The Error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt will mainly occurs if .dll files are missing so its better to put related .dll files or install upgraded office 2007,2010. this solves it.

We can Read E-mail Messages From Higher version of office like office 2007,2010 etc with the method as shown below.

outlook._Application X=new Outlook.Application();
Outlook.MAPIFolder=;  //...... Default Folderof outlook
String Path="......\...\temp.msg";
Outlook._MailItem Mail=(Outlook.MailItem)(X.Session.OpenSharedItem(Path);

you can Access Mail and Program it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top