C#Outlook 2010 - 使用'this.Application'时出错

时间:2013-09-16 21:49:12

标签: c# outlook outlook-2010

有谁能告诉我为什么我收到以下错误?谷歌没有太多帮助。对不起,这有点模糊。代码的目的是将所有未读的消息从Outlook中取出并转换为C#格式。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;

namespace TestEmailGetter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Outlook.MAPIFolder inbox = 
                this.Application.ActiveExplorer().Session.GetDefaultFolder
                (Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.Items unreadItems = inbox.Items.Restrict("[Unread]=true");

            MessageBox.Show(
                string.Format("Unread items in Inbox = {0}", unreadItems.Count));
        }
    }
}

错误:'TestEmailGetter.Form1'不包含'Application'的定义,也没有扩展方法'Application'接受类型为'TestEmailGetter.Form1'的第一个参数'(你是否缺少using指令或程序集)参考

此行发生错误:

this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

我不知道要添加什么来修复它。 : - (

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您的Form1课程没有成员Application。如果要从名称空间this引用Application类的静态成员,则必须在没有Microsoft.Office.Interop.Outlook的情况下使用它。

<强>更新

使用Outlook.Application ...

答案 1 :(得分:1)

要修复错误,请执行以下操作。您必须先创建OutlookApplication对象,然后才能在独立应用程序中使用它。

Outlook.Application OutlookApplication = new Outlook.Application();
Outlook.Folder calFolder =
   OutlookApplication.Session.GetDefaultFolder(
       Outlook.OlDefaultFolders.olFolderCalendar)
       as Outlook.Folder;