2016-05-19 3 views
0

私は方法DisplayAccountInformation()が呼び出されているthisのソリューションを使用しようとしています。C#でパラメータとして(Outlook.Applicationアプリケーション)を使用してメソッドを呼び出す方法は?

public partial class OutlookContacts : Form 
{ 
    public OutlookContacts() 
    { 
     InitializeComponent(); 
     //DisplayAccountInformation(??); 
    } 

    public static void DisplayAccountInformation(Outlook.Application application) 
    { 
     // The Namespace Object (Session) has a collection of accounts. 
     Outlook.Accounts accounts = application.Session.Accounts; 

     // Concatenate a message with information about all accounts. 
     var builder = new StringBuilder(); 

     // ..... 
     // [more code] 
     // ..... 

     // Display the account information. 
     System.Windows.Forms.MessageBox.Show(builder.ToString()); 
    } 


} 

ユーザー名とメールアドレスを含む連絡先のリストを取得しようとしています。

これはC#Windowsフォームアプリケーションです。

メソッドDisplayAccountInformation()を呼び出すにはどうすればよいですか?

+0

Outlook interopライブラリを参照してから、新しい「Outlook.Application」インスタンスを作成し、それを使用してログインしてから、このメソッドに渡す必要があります。 –

+0

これはアカウントとは何が関係していますか? Outlookアドレス帳と同じエントリを表示しようとしていますか? –

+0

@ YacoubMassad私はそれを持っています。私はちょうどコードを追加しなかった:Outlookを使用する= Microsoft.office.interop.outlook –

答えて

0

OutlookでMicrosoftのドキュメントを調査したところ、私が欲しい仕事をする解決策hereが見つかりました。そして今、私はそのセッションが間違っていたことを明確にしています。 @ドミトリーStreblechenko、@ヤコブマサドあなたが使用して右だったApplication.Session.AddressLists。 だから、以下の私のコードです:私は1つのDataGridViewを持つフォームOutlookContactsで

public partial class OutlookContacts : Form 
     { 
      private BindingSource supplierDataBindingSource = new BindingSource(); 

      public static IEnumerable<Employees> displayListOfOutLookUsers = new List<Employees>(); 
      public OutlookContacts() 
      { 
       InitializeComponent(); 
       dataGridView1.DataSource = supplierDataBindingSource; 
       displayListOfOutLookUsers = EnumerateGAL(); 

      } 
      private IEnumerable<Employees> EnumerateGAL() 
      { 
       Outlook.Application ol = new Outlook.ApplicationClass(); 
/*Outlook.AddressList gal = Application.Session.GetGlobalAddressList(); won't work because '_Application' needs to be instantiated using an object before using. */ 
       Outlook.AddressList gal = ol.Session.GetGlobalAddressList(); 
/*Declaring a list emp of type Employees.*/ 
      List<Employees> emp = new List<Employees>(); 
      if (gal != null) 
      { 
       for (int i = 1; 
        i <= Math.Min(100, gal.AddressEntries.Count - 1); i++) 
       { 
        Outlook.AddressEntry addrEntry = gal.AddressEntries[i]; 
        if (addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry 
         || addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry) 
        { 
         Outlook.ExchangeUser exchUser =addrEntry.GetExchangeUser(); 

/*Storing fetched records in the list.*/ 
         emp.Add(new Employees {EmployeeName = exchUser.Name,EmployeeEmail = exchUser.PrimarySmtpAddress}); 

        } 
        if (addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry) 
        { 
         Outlook.ExchangeDistributionList exchDL = addrEntry.GetExchangeDistributionList(); 

        } 
       } 
      } 
      displayListOfOutLookUsers = emp; 
      supplierDataBindingSource.DataSource = displayListOfOutLookUsers.Select(l => new { l.EmployeeName, l.EmployeeEmail }); 
      dataGridView1.AutoResizeColumns(
        DataGridViewAutoSizeColumnsMode.DisplayedCells); 
      dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; 
      int j = 0; 
      foreach (DataGridViewColumn c in dataGridView1.Columns) 
      { 
       c.SortMode = DataGridViewColumnSortMode.Automatic; 
       j += c.Width; 
      } 
      dataGridView1.Width = j + dataGridView1.RowHeadersWidth + 232; 
      this.Width = dataGridView1.Width + 40; 
      return displayListOfOutLookUsers; 
     } 

、と私はすべてのExchangeユーザーの名前と電子メールアドレスでそれを埋めるために望んでいました。

0

代わりにApplication.Session.AddressListsを使用してください。 OutlookSpyのオブジェクトで再生することができます。名前空間ボタンをクリックし、AddressListsプロパティを選択し、参照をクリックして、IEnumVariantタブに移動します。

関連する問題