2016-05-30 97 views
0

我有一個使用COM接口與Outlook進行通信的應用程序。找到所有帳戶中的所有Outlook日曆的最佳方法

我有一個下拉框,其中包含所有帳戶的所有日曆。

有一些報告的情況,在使用Exchange和Outlook 2010時,應用程序在加載日曆列表時掛起。這可能是因爲它與代碼(如下所示)或阻止訪問地址信息的防病毒有關?

它對於某些Exchange帳戶隨機失敗。

private void AppLoad(object sender, EventArgs e) 
{ 
    Outlook.Application msOutlook = new Outlook.Application(); 
    Outlook.NameSpace session = msOutlook.Session; 
    Outlook.Stores stores = session.Stores; 

    foreach (Outlook.MAPIFolder folder in session.Folders) 
    { 
     GetFolders(folder, msOutlook); 
    } 

    if(calendarSelector.Items.Count==0) 
    { 
     StoreAndCalendar ci = new StoreAndCalendar(); 
     ci.Text = "Default"; 
     ci.Account = "Noaccount"; 
     ci.Storename = "Noaccount"; 
     ci.Value = "Noaccount"; 
     calendarSelector.Items.Add(ci); 
    } 
    else 
    { 
     try 
     { 
      calendarSelector.SelectedIndex = 0; 
     } 
     catch { } 
    } 

    //preselect default calendar and account 
    GetDefaultCalendarAndAccount(); 
} 

public void GetFolders(Outlook.MAPIFolder folder, Outlook.Application app) 
{ 
     if (folder.Folders.Count == 0) 
     { 
      if (folder.DefaultItemType == Outlook.OlItemType.olAppointmentItem) 
      { 
       StoreAndCalendar ci = new StoreAndCalendar(); 
       Outlook.Account acc = GetAccountForFolder(folder, app); 
       if (acc != null) 
       { 
        if (!folder.FullFolderPath.Contains("Deleted")) 
        { 
         ci.Text = folder.Name + " - " + acc.DisplayName; 
         ci.Account = acc.DisplayName; 
         ci.Storename = acc.UserName; 
         ci.Value = folder.Name; 
         calendarSelector.Items.Add(ci); 
        } 
       } 
      } 
     } 
     else 
     { 
      if (folder.DefaultItemType == Outlook.OlItemType.olAppointmentItem) 
      { 
       StoreAndCalendar ci = new StoreAndCalendar(); 
       Outlook.Account acc = GetAccountForFolder(folder, app); 
       if (acc != null) 
       { 
        if (!folder.FullFolderPath.Contains("Deleted")) 
        { 
         ci.Text = folder.Name + " - " + acc.DisplayName; 
         ci.Account = acc.DisplayName; 
         ci.Storename = acc.UserName; 
         ci.Value = folder.Name; 
         calendarSelector.Items.Add(ci); 
        } 
       } 
      } 
      foreach (Outlook.MAPIFolder subFolder in folder.Folders) 
      { 
       GetFolders(subFolder, app); 
      } 
     } 
} 

Outlook.Account GetAccountForFolder(Outlook.MAPIFolder folder, Outlook.Application app) 
{ 
     // Obtain the store on which the folder resides. 
     Outlook.Store store = folder.Store; 

     // Enumerate the accounts defined for the session. 
     foreach (Outlook.Account account in app.Session.Accounts) 
     { 
      // Match the DefaultStore.StoreID of the account 
      // with the Store.StoreID for the currect folder. 
      if (account.DeliveryStore.StoreID == store.StoreID) 
      { 
       // Return the account whose default delivery store 
       // matches the store of the given folder. 
       return account; 
      } 
     } 
     // No account matches, so return null. 
     return null; 
} 


public void GetDefaultCalendarAndAccount() 
{ 
     try 
     { 
      Outlook.Application OutlookApp = new Outlook.Application(); 
      Outlook.NameSpace ns; 
      Outlook.MAPIFolder defaultfolder; 
      Outlook.Account defaultaccount; 
      ns = OutlookApp.Session; 
      ns.SendAndReceive(false); 
      defaultfolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar); 
      defaultaccount = GetAccountForFolder(defaultfolder, OutlookApp); 
      String defaultaccandfold = defaultfolder.Name + " - " + defaultaccount.DisplayName; 
      int index = 0; 
      for (int i = 0; i < calendarSelector.Items.Count; i++) 
      { 
       string value = calendarSelector.GetItemText(calendarSelector.Items[i]); 
       if (value == defaultaccandfold) 
       { 
        calendarSelector.SelectedIndex = index; 
       } 
       index++; 
      } 
     } 
     catch { } 
} 

通過帳戶及其所有子文件夾,列出所有有效日曆的最佳方式是什麼?

此外,選擇默認日曆:是它必須完成的方式?

謝謝!

+0

掛在哪一行?你有沒有試過調試你的代碼? –

+0

無法進行調試,因爲它不在開發人員計算機上。它只是掛起,但如果關閉Outlook,它會加載組合框中的日曆,這是奇怪的事情。 – Laureant

+0

如果無法在調試器下運行,請將日誌記錄添加到您的代碼中。 –

回答

0

你對目標是最好的方法。但是一些建議:

  • 沒有必要通過賬戶進行迭代,只是解析房屋
  • ,除非你想那些各種掛曆不處理對非電子郵件存儲對象,如SharePoint列表和Internet日曆百貨( )
  • 不要使用的foreach與Outlook對象模型循環,使用循環,所以你可以顯式調用Marshal.ReleaseComObject的每個引用了Outlook對象變量
  • 您對NameSpace.SendAndReceive呼叫可能是不必要的,並且可能是造成一些延誤
相關問題