【问题标题】:Outlook code compile error due to `Application` reference missing由于缺少“应用程序”引用,Outlook 代码编译错误
【发布时间】:2015-07-14 14:43:07
【问题描述】:

我正在尝试编写一些 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 应该是上述对象库的一部分,但显然它在这里不起作用。有人可以建议我做错了什么吗?

【问题讨论】:

    标签: c# outlook


    【解决方案1】:

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

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

    然后将其用作:

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

    【讨论】:

    • 谢谢,到目前为止这似乎有效。但我不清楚为什么 MS 示例中不需要 Application 对象。你能解释一下有什么区别吗?
    【解决方案2】:

    您使用了错误的项目。 在 Visual Studio 中创建新项目时,请使用 Outlook 加载项模板。 (模板 -> Visual C# -> Office -> Outlook)。

    在这段代码中,它们 Application.Session 将像您期望的那样工作。

    或者你应该像这样创建一个新的应用程序对象。 var outlook = new Microsoft.Office.Interop.Outlook.Application(); 并使用 Outlook.Session。

    【讨论】:

    • 谢谢,新的Application 对象方法似乎有效。 (不同的项目类型方法也可能有效,但我还没有尝试过。)您能否解释一下不同的项目类型的作用,以及为什么我不需要使用这种方法的Application 对象?鉴于 MS 示例没有指定 Application 对象,我想这一定是他们假设的?
    【解决方案3】:

    在文件开头添加以下行:

     using Microsoft.Office.Interop.Outlook;
    

    或者只是在任何 Outlook 对象声明前加上 Outlook 别名。

    您可能会发现C# app automates Outlook (CSAutomateOutlook) 示例项目很有帮助。

    【讨论】:

    • 抱歉,这似乎不起作用:非静态字段、方法或属性“Microsoft.Office.Interop.Outlook._Application.Session.get”需要对象引用但我尚未通读您提供的链接 - 所以原因可能在那里。我会仔细看看。
    猜你喜欢
    • 2020-05-07
    • 2019-11-21
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多