无法获取Outlook.Application在Outlook运行,直到非Outlook窗口是在前景

问题描述 投票:4回答:1

Marshal.GetActiveObject("Outlook.Application")抛出Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))在Outlook启动,并继续在其运行时,直到非Outlook窗口成为活动前台窗口。

我在Windows 10版本1803构建17134.523运行Outlook 1901版本构建11231.20130(这是我的本地机器)。我观察到不行为这种方式的客户机。这是通过使用经由的NuGet获得.NET 4.5.2和Microsoft.Office.Interop.Outlook版本15.0.4797.1003在WPF应用程序发生。

我曾多次运行下面的代码。如上所详述的,它会成功一次Outlook的实例已被最小化或非Outlook窗口设置为前景,但它可以连续失败(下面输出)在此之前已经发生。我试图暂停〜20-30秒,以允许Outlook的时间来加载,但仍得到相同的结果。

    // Detect that the active window is an Outlook window
    Outlook.Application app = null;
    // Optional: Wait for Outlook to load 20-30 sec using Thread.Sleep
    try
    {
        Debug.WriteLine("app");
        app = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
        Debug.WriteLine("got app");
    }
    catch (Exception exception)
    {
        Debug.WriteLine(exception.Message);
    }
    // Do things with app

在Outlook运行时,我希望看到:

app
got app

如果将它设置到前台非Outlook窗口(即,不是一个资源管理器或检查员窗口),因为Outlook已开始出现。如果Outlook正在运行,并且一直在前台,我反而看到:

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

如在黑暗中拍摄,我试图这样做,当我看到这种情况发生了下面的“启动”互操作性:

bootApplication = new Microsoft.Office.Interop.Outlook.Application();

没有效果。任何投入将受到重视。谢谢!

c# .net outlook office-interop com-interop
1个回答
3
投票

不要使用Outlook使用GetActiveObject - 它是一个单独的,所以创建Outlook.Application对象的新实例会返回一个指向现有实例,如果它已经在运行。

© www.soinside.com 2019 - 2024. All rights reserved.