简体   繁体   English

如何使用Interop C#启动或附加到Outlook?

[英]How to start or attach to Outlook using Interop C#?

I'm trying to write a C# program that will either start a new instance of Outlook if none are running, or attach to the first instance if it happens to be running already. 我正在尝试编写一个C#程序,该程序将在没有运行Outlook的情况下启动Outlook的新实例,或者在碰巧已经运行的情况下附加到第一个实例。

I've found a previous discussion already that pretty much identifies what I'm trying to achieve. 我已经找到了前面的讨论已经是非常确定我想要实现的。 The problem with the Microsoft Solution is that some of my Outlook Plugins/Add-ins don't work properly, for example, iManage Filesite/Worksite. Microsoft解决方案的问题是我的某些Outlook插件/加载项无法正常工作,例如iManage Filesite / Worksite。 The plugin appears to be in the ribbon when Outlook starts, but some functionality is missing and if I try a search etc. Outlook stops responding and crashes. 当Outlook启动时,该插件似乎位于功能区中,但缺少某些功能,并且如果我尝试搜索等,则Outlook停止响应并崩溃。 The plugins work properly if outlook is started from the console or like: 如果从控制台启动Outlook或类似方法,则插件可以正常工作:

System.Diagnostics.Process.Start(@"OUTLOOK.exe");

But then I can never attach to the running Outlook instance because 但后来我再也无法附加到正在运行的Outlook实例,因为

application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;

Fails with a COM error (0x800401E3 MK_E_UNAVAILABLE). 失败,出现COM错误(0x800401E3 MK_E_UNAVAILABLE)。 I can only get around this error if I debug the application and step through it. 如果我调试应用程序并逐步解决它,我只能解决该错误。 I've tried allowing time for Outlook to start, switching the active window (changing focus), but none of these options work. 我尝试给Outlook启动时间,切换活动窗口(更改焦点),但是这些选项都不起作用。

The error message from GetActiveObject most likely means your code and Outlook are running in different security contexts. 来自GetActiveObject的错误消息很可能意味着您的代码和Outlook在不同的安全上下文中运行。 Make sure neither apps (or both) are running with elevated privileges (Run As Administrator). 确保两个(或两个)应用均未以提升的权限运行(以管理员身份运行)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM