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.