Office 相互運用 API を使用して、Outlook から保存された .msg ファイルを開き、返信ウィンドウを表示してユーザーが返信できるようにしています。
Office 2003 を実行している場合、OpenSharedItem(pathToMSGFile); を呼び出すと、次の例外がスローされます。
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)
Office 2008 を実行している場合、問題なく動作します。
小さなテストケースをまとめました。コードは次のとおりです。
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();
}
アプリケーションは、Office12 相互運用ライブラリを使用して、.Net 4 を対象としています。AnyCPU用にコンパイルされているかx86用にコンパイルされているかに関係なく、同じことが起こります。