简体   繁体   English

为什么 Outlook 在使用 Office 互操作时会崩溃?

[英]Why is Outlook crashing when using Office interop?

I am attempting to utilize Office interop with C#, but I'm having some difficulties.我正在尝试通过 C# 使用 Office 互操作,但我遇到了一些困难。 Executing a test like the one I included below seems to work insofar as it launches Outlook and seems to connect with it.执行像我在下面包含的测试一样的测试似乎可以正常工作,因为它启动了 Outlook 并似乎与之连接。 The issue is that if I then try to open the Outlook window (it starts hidden in the tray) I get an error message from Outlook saying The application was unable to start correctly (0xc0000142).问题是,如果我然后尝试打开 Outlook 窗口(它开始隐藏在托盘中),我会从 Outlook 收到一条错误消息,说The application was unable to start correctly (0xc0000142). I do not get this error if Outlook was already running before I started my application.如果在我启动应用程序之前 Outlook 已经在运行,我不会收到此错误。 Am I doing something incorrectly or is something broken?我做错了什么还是坏了?

using System;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookInteropTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            var app = new Outlook.Application();
            Console.ReadKey();
        }
    }
}

Visual Studio Community 2017 Version 15.2 Visual Studio 社区 2017 版本 15.2

Office 360 - Outlook Version 1804 Build 9226.2156 Office 360​​ - Outlook 版本 1804 内部版本 9226.2156

Windows 10 Build 17115.1 Windows 10 内部版本 17115.1

EDIT: Tested this on Windows 7 and could not reproduce crash.编辑:在 Windows 7 上对此进行了测试,但无法重现崩溃。 I know that I had this working in Windows 10 at some point.我知道我在某个时候在 Windows 10 中使用过这个。 I reinstalled my OS and it still crashes.我重新安装了我的操作系统,但它仍然崩溃。 I'm chocking this up to the typical Microsoft user experience unless anyone has any ideas on how to fix it.除非有人对如何修复它有任何想法,否则我将其与典型的 Microsoft 用户体验联系起来。

Outlook is a singleton, so creating a new object will return the existing object if Outlook is already running. Outlook 是一个单例,因此如果 Outlook 已在运行,则创建新对象将返回现有对象。

In your case you also need to provide namespace to it在您的情况下,您还需要为其提供命名空间

olApp = new Outlook.Application();
Outlook.Namespace ns = olApp.GetNamespace("MAPI");
ns.Logon();

I know this is old, but I was having the same issue and perhaps it will help someone in the future: As IAmRajshah mentioned, only one istance of outlook can run, so, if Outlook is open your code olApp = new Outlook.Application();我知道这是旧的,但我遇到了同样的问题,也许将来会对某人有所帮助:正如 IAmRajshah 提到的,只有一个 Outlook olApp = new Outlook.Application();可以运行,因此,如果 Outlook 已打开,您的代码olApp = new Outlook.Application(); will crash, you need to "connect" to the active instance of outlook with somenthing like this Oulook.Application olApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;会崩溃,你需要用类似这样的Oulook.Application olApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; “连接”到 Outlook 的活动实例Oulook.Application olApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; The link below has a good example of this:下面的链接有一个很好的例子:

Get and sign in to an instance of Outlook 获取并登录 Outlook 实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Office Interop启动Outlook时出现问题 - Issue launching Outlook using Office Interop 使用 Microsoft.Office.Interop.Outlook 在 Office 365 上创建 mailItem 时随机发生 RPC 错误 - RPC Error occurring randomly when creating mailItem on Office 365 using Microsoft.Office.Interop.Outlook 使用未安装Outlook客户端的Microsoft.Office.Interop.Outlook - Using Microsoft.Office.Interop.Outlook without Outlook Client installed 为什么Microsoft.Office.Interop.Outlook.Excecption存在? - Why does Microsoft.Office.Interop.Outlook.Excecption exist? 为什么不在ASP.NET中运行Microsoft.Office.Interop.Outlook? - Why not running Microsoft.Office.Interop.Outlook in asp.net? 检测“Office.Interop.Outlook”何时不发送邮件 - Detect when “Office.Interop.Outlook” don't send mail 尝试读取办公室 outlook 模板的内容时出现互操作异常 - interop exception when trying to read the contents of an office outlook template C#在不使用Office互操作的情况下创建Outlook任务 - C# Create Outlook task without using office interop 从C#Windows服务使用Outlook Office.Interop - Using Outlook Office.Interop from a C# windows service 使用Office Interop无法从Outlook中删除400个以上的联系人 - Unable to delete over 400 contacts from Outlook using Office Interop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM