Export (0) Print
Expand All

How to: Get Information About the Current User

This example shows how to get the current user’s information, such as name, job title, and telephone number.

Programming Applications for Office Outlook 2007

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007, from Microsoft Press (ISBN 9780735622494, copyright Microsoft Press 2007, all rights reserved).

Buy this book

Sample chapters

To obtain an ExchangeUser object from an AddressEntry object, call the GetExchangeUser() method on the AddressEntry object. In the following procedure, GetCurrentUserInfo gets the AddressEntry property for the Recipient object by using the CurrentUser property. If the AddressEntry object represents an Exchange mailbox user, GetCurrentUserInfo calls the GetExchangeUser method and an ExchangeUser object is returned. The Name, PrimarySmtpAddress, JobTitle, Department, OfficeLocation, BusinessTelephoneNumber, and MobileTelephoneNumber properties are written to the trace listeners of the Listeners collection.

If you use Microsoft Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 14.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void GetCurrentUserInfo()
{
    Outlook.AddressEntry addrEntry =
        Application.Session.CurrentUser.AddressEntry;
    if (addrEntry.Type == "EX")
    {
        Outlook.ExchangeUser currentUser =
            Application.Session.CurrentUser.
            AddressEntry.GetExchangeUser();
        if (currentUser != null)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Name: "
                + currentUser.Name);
            sb.AppendLine("STMP address: "
                + currentUser.PrimarySmtpAddress);
            sb.AppendLine("Title: "
                + currentUser.JobTitle);
            sb.AppendLine("Department: "
                + currentUser.Department);
            sb.AppendLine("Location: "
                + currentUser.OfficeLocation);
            sb.AppendLine("Business phone: "
                + currentUser.BusinessTelephoneNumber);
            sb.AppendLine("Mobile phone: "
                + currentUser.MobileTelephoneNumber);
            Debug.WriteLine(sb.ToString());
        }
    }
}

Other Resources

Show:
© 2014 Microsoft