c# - 由于缺少 `Application` 引用,Outlook 代码编译错误

标签 c# outlook

我正在尝试编写一些 C# 代码来与 Outlook 2010 进行交互。我目前正在使用 this example from Microsoft .

我的代码如下:

using System;
using System.Text;          // StringBuilder
using System.Diagnostics;   // Debug

using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

using Outlook = Microsoft.Office.Interop.Outlook;

namespace DirectReports
{
    public class Program
    {
        private void GetManagerDirectReports()
        {
            Outlook.AddressEntry currentUser = Application.Session.CurrentUser.AddressEntry;
            //Outlook.AddressEntry currentUser = Outlook.Application.Session.CurrentUser.AddressEntry;
            if (currentUser.Type == "EX")
            {
                Outlook.ExchangeUser manager = currentUser.GetExchangeUser().GetExchangeUserManager();
                if (manager != null)
                {
                    Outlook.AddressEntries addrEntries =
                        manager.GetDirectReports();
                    if (addrEntries != null)
                    {
                        foreach (Outlook.AddressEntry addrEntry
                            in addrEntries)
                        {
                            Outlook.ExchangeUser exchUser = addrEntry.GetExchangeUser();
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("Name: " + exchUser.Name);
                            sb.AppendLine("Title: " + exchUser.JobTitle);
                            sb.AppendLine("Department: " + exchUser.Department);
                            sb.AppendLine("Location: " + exchUser.OfficeLocation);
                            Debug.WriteLine(sb.ToString());
                        }
                    }
                }
            }
        }
    }
}

Microsoft 示例提到“如果您使用 Visual Studio 测试此代码示例,则必须首先添加对 Microsoft Outlook 15.0 对象库组件的引用”。我在 Windows 桌面的 Visual Studio Express 2013 中工作。我没有看到 15.0 版的对象库,而是添加了 14.0 版的对象库(无论如何我认为它适合 Outlook 2010):

references included

当我尝试构建时,出现以下错误:

The name 'Application' does not exist in the current context

我阅读了一些引用资料,指出 Application 应该是上述对象库的一部分,但显然它在这里不起作用。有人可以建议我做错了什么吗?

最佳答案

您可以创建一个新的 Application 对象:

var appOutlook = new Microsoft.Office.Interop.Outlook.Application();

然后将其用作:

Outlook.AddressEntry currentUser = appOutlook.Session.CurrentUser.AddressEntry;

关于c# - 由于缺少 `Application` 引用,Outlook 代码编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31409868/

相关文章:

c# - 无法转换类型为 'Microsoft.Office.Interop.Outlook.ApplicationClass' {00063001-0000-0000-C000-000000000046} HRESULT : 0x80040155 的 COM 对象

c# - 通过 C# 应用程序创建 Outlook 电子邮件草稿

HTML 电子邮件样式在 Outlook 中不起作用

c# - 根据消息元数据过滤 ServiceBusTrigger 消息

c# - 菜单和更新到主 Pane (WPF)

c# - 从 javascript 函数调用 .cs 文件内的程序以实现 onkeypress 函数 ASP.net

c# - 如何在托管 Windows 服务中托管 WCF 服务?

html - 显示与浏览器不同的简单电子邮件 CSS

c# - 将负数转换为无符号类型(ushort、uint 或 ulong)

c# - 为什么对单例使用 Lazy<T> 而不是静态工厂类?