如何使用互操作向Outlook日历添加新的会议请求?

时间:2012-02-09 19:27:15

标签: c# interop outlook

好的,我正在尝试使用Interop连接到C#中的共享Outlook日历并添加新的会议请求。

这是我到目前为止所做的事情,从我的using语句开始(这是一种Windows形式):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

然后我有一个公共课叫做#34;约会"以下是:

public class Appointments
{
    public string ConversationTopic { get; set; }
    public int Duration { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public string Organizer { get; set; }
    public int ReminderMinutesBeforeStart { get; set; }
    public string RequiredAttendees { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

我有一个新的空白窗口表单,其中包含一个当前名为dataGridView1的数据网格视图。表单加载事件代码如下:

private void Form1_Load(object sender, EventArgs e)
{
    Outlook.Application oApp;
    oApp = new Outlook.Application();
    Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
    oNS.Logon(Missing.Value, Missing.Value, true, true);

    Outlook.Recipient oRecip = (Outlook.Recipient)oNS.CreateRecipient("Foo bar");
    Outlook.MAPIFolder oFolder = (Outlook.MAPIFolder) oNS.GetSharedDefaultFolder(oRecip, Outlook.OlDefaultFolders.olFolderCalendar);

    List<Appointments> appointmentList = new List<Appointments>();

    foreach (object item in oFolder.Items)
    {
        Outlook.AppointmentItem thisOne = (Outlook.AppointmentItem)item;
        appointmentList.Add(new Appointments { ConversationTopic = thisOne.ConversationTopic, Duration = thisOne.Duration, EndTime = thisOne.End, Organizer = thisOne.Organizer, ReminderMinutesBeforeStart = thisOne.ReminderMinutesBeforeStart, RequiredAttendees = thisOne.RequiredAttendees, StartTime = thisOne.Start, Subject = thisOne.Subject, Body = thisOne.Body });
    }

    dataGridView1.AutoGenerateColumns = true;
    dataGridView1.DataSource = appointmentList;
    dataGridView1.Sort(dataGridView1.Columns["Subject"], ListSortDirection.Descending);
}

这可以完美地连接到我的日历并使用我的所有相关日历信息填充我的数据网格视图。现在,我想以编程方式将新的会议请求发送到日历。

我猜测会议请求是一个oFolder.Item所以我想要输入:

oFolder.Items.Add(* details here *);

在括号内,intellisense简单地说出以下内容:

  

dynamic_Items.Add([object Type = Type.Missing])

现在我感到难过,非常感谢帮助。

由于

1 个答案:

答案 0 :(得分:10)

using Outlook = Microsoft.Office.Interop.Outlook;    

private void SetRecipientTypeForAppt()
    {
        Outlook.AppointmentItem appt =
            Application.CreateItem(
            Outlook.OlItemType.olAppointmentItem)
            as Outlook.AppointmentItem;
        appt.Subject = "Customer Review";
        appt.MeetingStatus = Outlook.OlMeetingStatus.olMeeting;
        appt.Location = "36/2021";
        appt.Start = DateTime.Parse("10/20/2006 10:00 AM");
        appt.End = DateTime.Parse("10/20/2006 11:00 AM");
        Outlook.Recipient recipRequired =
            appt.Recipients.Add("Ryan Gregg");
        recipRequired.Type =
            (int)Outlook.OlMeetingRecipientType.olRequired;
        Outlook.Recipient recipOptional =
            appt.Recipients.Add("Peter Allenspach");
        recipOptional.Type =
            (int)Outlook.OlMeetingRecipientType.olOptional;
        Outlook.Recipient recipConf =
           appt.Recipients.Add("Conf Room 36/2021 (14) AV");
        recipConf.Type =
            (int)Outlook.OlMeetingRecipientType.olResource;
        appt.Recipients.ResolveAll();
        appt.Display(false);
    }

通过如何:Create a Meeting Request, Add Recipients, and Specify a Location