我正在使用 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();
}
该应用程序的目标是 .Net 4,使用 Office12 互操作库。无论是为 AnyCPU 还是 x86 编译,都会发生同样的情况。