現在のフォルダーに基づいて特定のアカウントの送信可能なアイテムを作成する (Outlook)

Application オブジェクトの CreateItem メソッドを使用して Microsoft Outlook アイテムを作成すると、そのセッションのプライマリ アカウント用にアイテムが作成されます。 プロファイルで複数のアカウントが定義されているセッションでは、特定の IMAP、POP、または Microsoft Exchange の各アカウントのアイテムを作成できます。 ユーザー インターフェイスを使用 (たとえば、[新しいメール] または [新しい会議] をクリック) して送信可能なアイテムを作成するときに現在のプロファイルに複数のアカウントがある場合、インスペクターは新しいメールまたは会議出席依頼を作成モードで表示し、それらのアイテムの送信元アカウントを選択することができます。 このトピックでは、プログラムを使用して送信可能なアイテムを特定の送信元アカウントで作成する方法を説明します。 2 つのコード例では、アクティブなエクスプローラーの現在のフォルダーによって決定される特定のアカウントの MailItem および AppointmentItem を作成する方法を示します。

次のマネージ コードは C# で作成されています。 コンポーネント オブジェクト モデル (COM) に呼び出す必要がある .NET Framework マネージ コード サンプルを実行するには、マネージ インターフェイスを定義し、オブジェクト モデル タイプ ライブラリの COM オブジェクトにマップする相互運用機能アセンブリを使用する必要があります。 Outlook の場合、Visual Studio および Outlook プライマリ相互運用機能アセンブリ (PIA) を使用できます。 Outlook 2013 用のマネージ コード サンプルを実行する前に、Outlook 2013 PIA をインストールしており、Visual Studio で Microsoft Outlook 15.0 オブジェクト ライブラリ コンポーネントへの参照を追加していることを確認してください。 Outlook アドインのクラスで次のコードを ThisAddIn 使用する必要があります (Office Developer Tools for Visual Studio を使用)。 コードの Application オブジェクトは で提供された、信頼済み Outlook ThisAddIn.Globals オブジェクトである必要があります。 Outlook PIA を使用してマネージド Outlook ソリューションを開発する方法の詳細については、MSDN の 「Outlook プライマリ相互運用機能アセンブリ リファレンスへようこそ」を参照 してください。

次の最初の方法は、 CreateMailItemFromAccount特定のアカウントの MailItem を 作成し、作成モードで表示します。特定のアカウントの既定の配信ストアは、アクティブなエクスプローラーに表示されるフォルダーのストアと同じです。 アカウントの現在のユーザーが送信者として設定されます。 CreateMailItemFromAccount 最初に、現在のフォルダーのストア (Folder.Store プロパティから取得) と、セッションの Accounts コレクションで定義されている各アカウントの既定の配信ストア ( Account.DeliveryStore プロパティで取得) を照合することで、適切な アカウント を識別します。 CreateMailItemFromAccount 次に、 MailItem を作成します。 アイテムをアカウントに関連付けるには、CreateMailItemFromAccountアカウントのユーザーの AddressEntry オブジェクトを MailItem の Sender プロパティに設定して、アカウントのユーザーをアイテムの送信者として割り当てます。 Sender プロパティの割り当ては重要な手順です。そうしないと、プライマリ アカウントに対して MailItem が作成されます。 メソッドの最後に、 CreateMailItemFromAccountMailItem を表示します。 現在のフォルダーが配信ストアにない場合は、 CreateMailItemFromAccount セッションのプライマリ アカウントの MailItem を作成するだけです。

private void CreateMailItemFromAccount() 
{ 
    Outlook.AddressEntry addrEntry = null; 
 
    // Get the store for the current folder. 
    Outlook.Folder folder = 
        Application.ActiveExplorer().CurrentFolder  
        as Outlook.Folder; 
    Outlook.Store store = folder.Store; 
     
    Outlook.Accounts accounts = 
        Application.Session.Accounts; 
 
    // Match the delivery store of each account with the  
    // store for the current folder. 
    foreach (Outlook.Account account in accounts) 
    { 
        if (account.DeliveryStore.StoreID ==  
            store.StoreID) 
        { 
            addrEntry = 
                account.CurrentUser.AddressEntry; 
            break; 
        } 
    } 
 
    // Create MailItem. Account is either the primary 
    // account or the account with a delivery store 
    // that matches the store for the current folder. 
    Outlook.MailItem mail = 
        Application.CreateItem( 
        Outlook.OlItemType.olMailItem) 
        as Outlook.MailItem; 
 
    if (addrEntry != null) 
    { 
        //Set Sender property. 
        mail.Sender = addrEntry; 
        mail.Display(false); 
    } 
} 

次のメソッド は、CreateMeetingRequestFromAccountMailItem ではなく AppointmentItem を作成し、SendUsingAccount プロパティを使用して AppointmentItem をアカウントに関連付ける点を除いて、 と似ていますCreateMailItemFromAccountCreateMeetingRequestFromAccount は、既定の配信ストアがアクティブなエクスプローラーに表示されるフォルダーのストアと同じアカウントの予定表フォルダーに AppointmentItem を作成します。 CreateMeetingRequestFromAccount 最初に、現在のフォルダーのストア (Folder.Store プロパティから取得) と、セッションの Accounts コレクションで定義されている各アカウントの既定の配信ストア ( Account.DeliveryStore プロパティで取得) を照合することで、適切な アカウント を識別します。 CreateMeetingRequestFromAccount 次に、 AppointmentItem を作成します。 アイテムをアカウントに関連付けるには、 CreateMeetingRequestFromAccount Account オブジェクトを AppointmentItemSendUsingAccount プロパティに設定して、そのアカウントをアイテムの送信アカウントとして割り当てます。 SendUsingAccount プロパティを割り当てることが重要な手順です。そうしないと、プライマリ アカウントに AppointmentItem が作成されます。 メソッドの最後に AppointmentItemCreateMeetingRequestFromAccount を表示します。 現在のフォルダーが配信ストアにない場合は、 CreateMeetingRequestFromAccount セッションのプライマリ アカウントの AppointmentItem を作成するだけです。

private void CreateMeetingRequestFromAccount() 
{ 
    Outlook.Account acct = null; 
 
    // Get the store for the current folder. 
    Outlook.Folder folder = 
        Application.ActiveExplorer().CurrentFolder 
        as Outlook.Folder; 
    Outlook.Store store = folder.Store; 
 
    Outlook.Accounts accounts = 
        Application.Session.Accounts; 
 
    // Match the delivery store of each account with the  
    // store for the current folder. 
    foreach (Outlook.Account account in accounts) 
    { 
        if (account.DeliveryStore.StoreID == 
            store.StoreID) 
        { 
            acct = account; 
            break; 
        } 
    } 
  
    // Create AppointmentItem. Account is either the primary 
    // account or the account with a delivery store 
    // that matches the store for the current folder. 
    Outlook.AppointmentItem appt = 
        Application.CreateItem( 
        Outlook.OlItemType.olAppointmentItem) 
        as Outlook.AppointmentItem; 
 
    appt.MeetingStatus =  
        Outlook.OlMeetingStatus.olMeeting; 
    if (acct != null) 
    { 
        //Set SendUsingAccount property. 
        appt.SendUsingAccount=acct; 
        appt.Display(false); 
    } 
} 

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。