C#在VSTO加载项中获取正在运行的Outlook实例 [英] C# Get running Outlook instance in VSTO add-in

查看:129
本文介绍了C#在VSTO加载项中获取正在运行的Outlook实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Excel加载项中获取一个Outlook Application对象.

I am trying to get an Outlook Application object in my add-in for Excel.

如果有一个正在运行的Outlook实例,它应该得到它,如果没有,则应该使用Outlook对象模型创建一个实例.

If there's a running Outlook instance, it should get that, if there isn't any, it should create one, using the Outlook object model.

这是我现在拥有的代码:

This is the code I have right now:

public static Outlook.Application GetApplicationObject()
{
    Outlook.Application application = null;

    if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
    {
        application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
    }
    else
    {
        application = new Outlook.Application();
    }
    return application;
}

我的问题:它找到Outlook进程,但找不到它们,并抛出以下错误消息:

My problem: it finds Outlook processes, but can't get them, throwing the following error message:

操作不可用(HRESULT异常:0x800401E3(MK_E_UNAVAILABLE))

Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))

我尝试逐步调试它,并监视了任务管理器.我可以看到我有一个Outlook实例,但这只是任务栏右侧的一个图标.这是否意味着该实例尚未完全加载,并且无法从该实例中获取Application对象,所以无法对其进行访问?

I tried debugging it step by step, and monitored the task manager. I could see that I have an Outlook instance, but it's only an icon in the right side of the taskbar. Does this mean, that the instance is not fully loaded yet, and it can't be accessed, to get the Application object from it?

我最终修改了我的代码,并将if-else分为2个try-catches,并带有各自的返回值,但是我仍然认为上面的代码应该可用.

I ended up modifying my code, and separating the if-else into 2 try-catches, with their own returns, but I still think that the code above should be usable.

推荐答案

Outlook是一个单例,因此new Outlook.Application()将始终有效-如果它已经在运行,则将获得该正在运行的对象.

Outlook is a singleton, so new Outlook.Application() will always work - if it is already running, you will get that running object.

确保两个应用程序(Excel和Outlook)都在相同的安全上下文中运行.任一应用程序是否都以提升的特权运行(以管理员身份运行)?

Make sure both apps (Excel and Outlook) are running in the same security context. Is either app running with elevated privileges (Run As Administrator)?

这篇关于C#在VSTO加载项中获取正在运行的Outlook实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆