我正在尝试编写一些 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):
当我尝试构建时,出现以下错误:
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/