2013-01-17 73 views
1

我正在編寫將使用Outlook API將日曆項目從共享日曆複製到我的個人日曆的應用程序。以下是我迄今爲止...使用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(); 
     } 
    } 

我能夠返回從三個日曆項的列表,但現在我需要將它們複製到我的個人日曆,這是我在哪裏卡住了。任何人都知道如何去做這件事?

謝謝!

託尼

回答

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()時通過目標文件夾來避免中間步驟。