次の方法で共有


現在のユーザーの情報を取得する

この例では、現在のユーザーの情報 (名前、役職、電話番号など) を取得する方法を示します。

注:

次のコード サンプルは、『Programming Applications for Microsoft Office Outlook 2007』からの抜粋です。

ExchangeUser オブジェクトを AddressEntry オブジェクトから取得するには、AddressEntry オブジェクトの GetExchangeUser() メソッドを呼び出します。 次のプロシージャで GetCurrentUserInfo は、CurrentUser プロパティを使用して、Recipient オブジェクトの AddressEntry プロパティを取得します。 AddressEntry オブジェクトが Exchange メールボックス ユーザーを表している場合、GetCurrentUserInfo は GetExchangeUser メソッドを呼び出し、ExchangeUser オブジェクトが返されます。 NamePrimarySmtpAddressJobTitleDepartmentOfficeLocationBusinessTelephoneNumber、および MobileTelephoneNumber の各プロパティが、Listeners コレクションのトレース リスナーに書き出されます。

Visual Studio を使用してこのコード例をテストする場合、Microsoft.Office.Interop.Outlook 名前空間をインポートするときに、まず Microsoft Outlook 15.0 オブジェクト ライブラリ コンポーネントへの参照を追加し、Outlook 変数を指定します。 using ステートメントは、コード例の関数の前に直接置くことはできません。パブリッククラス宣言の前に追加する必要があります。 次のコード行は、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());
        }
    }
}

関連項目