使用Outlook API将日历项目从共享日历复制到个人日历

时间:2013-01-17 11:57:24

标签: c# calendar outlook

我正在编写一个应用程序,该应用程序将使用Outlook API将日历项目从共享日历复制到我的个人日历。这是我到目前为止所拥有的......

using Outlook = Microsoft.Office.Interop.Outlook;

public Outlook.Items GetPublicEntries(string calendar)
    {
        Microsoft.Office.Interop.Outlook.Items CalendarFolderItems = null;
        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(calendar);
        Outlook.MAPIFolder usersCalendarFolder = (Outlook.MAPIFolder)oNS.GetSharedDefaultFolder(oRecip, Outlook.OlDefaultFolders.olFolderCalendar);
        CalendarFolderItems = usersCalendarFolder.Items;
        return CalendarFolderItems;
    }



    static void Main(string[] args)
    {
        String[] Cals = { "Appointments","Deadlines","Hearings"};

        foreach (string cal in Cals)
        {
            CalendarItems calobj = new CalendarItems();
            Outlook.Items calobjs = calobj.GetPublicEntries(cal);

            foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in calobjs)
            {
                try
                {
                    Console.WriteLine(item.Subject + " -> " + item.Start.ToLongDateString() + " - " + item.GlobalAppointmentID);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            Console.ReadKey();
        }
    }

我可以从三个日历中返回一个项目列表,但现在我需要将它们复制到我的个人日历中,这就是我被困住的地方。任何人都知道如何做到这一点?

谢谢!

1 个答案:

答案 0 :(得分:0)

Outlook不允许您直接复制到指定的文件夹(不像AppointmentItem.Move(),它将MAPIFolder对象作为参数) - 使用AppointmentItem.Copy,然后是AppointmentItem.Move:

AppointmentItem copiedItem = Item.Copy();
AppointmentItem newItem = copiedItem.Move(YourDestinationFolder);
newItem.Save();

请注意,当您调用Copy()时Outlook将清除全局约会ID - 这意味着即使邮件直接发送到您的收件箱,邮件更新也无法找到约会。

如果您使用Redemption,则可以避免中间步骤 - 它的RDOAppointmentItem(从RDOMail对象派生)允许您在调用CopyTo()时传递目标文件夹。