由于缺少"Application"引用而导致Outlook代码编译错误

Rob*_*rto 1 c# outlook

我正在尝试编写一些c#代码与Outlook 2010进行交互.我目前正在使用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());
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Microsoft示例提到"如果使用Visual Studio测试此代码示例,则必须首先添加对Microsoft Outlook 15.0对象库组件的引用".我正在使用Visual Studio Express 2013 for Windows Desktop.我没有看到15.0版本的对象库,而是添加了14.0版本(我觉得它对于Outlook 2010来说是正确的):

参考包括

当我尝试构建时,我收到以下错误:

The name 'Application' does not exist in the current context
Run Code Online (Sandbox Code Playgroud)

我读了几个引用,表明它们Application应该是上述对象库的一部分,但显然它在这里不起作用.有人可以建议我做错了吗?

tez*_*zzo 6

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

var appOutlook = new Microsoft.Office.Interop.Outlook.Application();
Run Code Online (Sandbox Code Playgroud)

然后将其用作:

Outlook.AddressEntry currentUser = appOutlook.Session.CurrentUser.AddressEntry;
Run Code Online (Sandbox Code Playgroud)