How to invoke a method with (Outlook.Application application) as parameter in C#?

馋奶兔 提交于 2021-02-08 11:21:05

问题


I am trying to use this solution in which method DisplayAccountInformation() is being called.

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());
    }


}

I am trying to get the list of contacts including user name and email address.

It's a C# Windows Forms application.

How do I invoke the method DisplayAccountInformation()?


回答1:


Use Application.Session.AddressLists instead. You can play with the objects in OutlookSpy: click Namespace button, select AddressLists property, click Browse, go to the IEnumVariant tab. etc.




回答2:


After some research and going through Microsoft's documentations on Outlook, I found a solution here which does just the job I want. And now I am clear what the mistake was with the "Session". @Dmitry Streblechenko , @ Yacoub Massad you were right about using Application.Session.AddressLists. So below is my code :

    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;
        }

In the form OutlookContacts I have one datagridview, and I wanted to fill it with all the Exchange User's Name and Email Addresses.



来源:https://stackoverflow.com/questions/37334242/how-to-invoke-a-method-with-outlook-application-application-as-parameter-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!