2016-05-30 5 views
0

私は、Outlookと通信するためにCOMインターフェイスを使用するアプリケーションを持っています。すべての勘定科目ですべての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リストやインターネット予定表(のような、非電子メールストアオブジェクトの店舗を処理しない解析し、ありません)
  • 明示的NameSpace.SendAndReceiveへのあなたの呼び出しは、おそらく不要であり、可能性があり
  • Outlookオブジェクトを参照するすべての変数にMarshal.ReleaseComObjectを呼び出すことができますので、forループを使用、foreachのは、Outlookオブジェクトモデルでループを使用しないでください。若干の遅延を引き起こす
関連する問題