编程语言
首页 > 编程语言> > c# – 带有Outlook互操作的OpenSharedItem在Office 2003中引发奇怪的异常,适用于Office 2008

c# – 带有Outlook互操作的OpenSharedItem在Office 2003中引发奇怪的异常,适用于Office 2008

作者:互联网

我正在使用Office interop API打开从outlook保存的.msg文件,然后显示一个回复窗口以允许用户回复它.

运行Office 2003时,OpenSharedItem(pathToMSGFile); call抛出以下异常:

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编译,都会发生同样的情况.

解决方法:

我注意到互操作库的版本11不包含OpenSharedItem()方法.

它从版本12开始可用.

看来这个操作在Office 11/2003上是不可用的(至少不能通过任何版本的互操作lib上的方法调用).

我不确定这是否适合您的场景,但我在Outlook Redemption库中取得了很大的成功.

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.

它似乎规避了不同版本的Outlook中的一些奇怪/不一致的行为(无论是通过设计还是作为原始目标的“副作用”).

如果您对CDO非常熟悉,那么您会对RDO感到满意.但说实话,我不知道它如何“映射”到Microsoft.Office.Interop.Outlook.

参见http://www.dimastr.com/redemption/rdo/rdosession.htm

Session.OpenSharedItem()的等效RDO函数是RDOSession.GetMessageFromMsgFile().

NB除了我偶尔使用它之外,我与这个产品没有任何关系!

标签:c,net,office-interop,outlook-2003
来源: https://codeday.me/bug/20190625/1288122.html