Share via


根據 Outlook) 目前資料夾建立特定帳戶的可傳送專案 (

當您使用Application物件的CreateItem方法來建立 Microsoft Outlook 專案時,會為該會話的主要帳戶建立專案。 在設定檔中已定義多個帳戶的工作階段中,您可以針對特定的 IMAP、POP 或 Microsoft Exchange 帳戶建立項目。 如果目前設定檔中有多個帳戶,而且您在使用者介面中建立可傳送的專案,例如,按一下[新增Email] 或 [新增會議],偵測器會以撰寫模式顯示新的訊息項目或會議邀請,然後您可以選取要從中傳送專案的帳戶。 本主題會示範如何以程式設計方式建立可傳送的項目,並使用特定的傳送帳戶來傳送該項目。 本主題有兩個程式碼範例,示範如何為使用中檔案總管中的目前資料夾所決定的特定帳戶建立 MailItemAppointmentItem

The following managed code is written in C#. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library. For Outlook, you can use Visual Studio and the Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2013, ensure that you have installed the Outlook 2013 PIA and have added a reference to the Microsoft Outlook 15.0 Object Library component in Visual Studio. 您應該使用 Office Developer Tools for Visual Studio) ,在 Outlook 增益集 (類別中使用下列程式碼 ThisAddIn 。 程式碼中的Application物件必須是 所 ThisAddIn.Globals 提供的受信任 Outlook應用程式物件。 如需使用 Outlook PIA 開發受控 Outlook 解決方案的詳細資訊,請參 閱 MSDN 上的歡迎使用 Outlook 主要 Interop 元件參考

下列 CreateMailItemFromAccount 第一個方法會建立特定帳戶的 MailItem ,並以撰寫模式顯示它;特定帳戶的預設傳遞存放區與使用中檔案總管中顯示之資料夾的存放區相同。 已將帳戶的目前使用者設定為寄件者。 CreateMailItemFromAccount首先,比對從Folder.Store 屬性取得之目前資料夾 (的存放區,) 每個帳戶的預設傳遞存放區, (使用會話的Accounts 集合中定義的Account.DeliveryStore屬性) 取得,來識別適當的帳戶。 CreateMailItemFromAccount 然後建立 MailItem。 若要將專案與帳戶產生關聯, CreateMailItemFromAccount 請將帳戶使用者的AddressEntry物件設定為MailItemSender屬性,將帳戶的使用者指派為專案的寄件者。 指定 Sender 屬性是一個重要步驟,因為,除此之外,系統還會針對主要帳戶建立 MailItem。 在 方法的結尾, CreateMailItemFromAccount 顯示 MailItem。 請注意,如果目前的資料夾不在傳遞存放區上, 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); 
    } 
} 

下一個方法 CreateMeetingRequestFromAccount 類似于 CreateMailItemFromAccount ,不同之處在于它會建立AppointmentItem而非MailItem,並使用其SendUsingAccount屬性將AppointmentItem與帳戶產生關聯。 CreateMeetingRequestFromAccount 會在帳戶的 [行事曆] 資料夾中建立 AppointmentItem ,該帳戶的預設傳遞存放區與使用中檔案總管中顯示之資料夾的存放區相同。 CreateMeetingRequestFromAccount首先,比對從Folder.Store 屬性取得之目前資料夾 (的存放區,) 每個帳戶的預設傳遞存放區, (使用會話的Accounts 集合中定義的Account.DeliveryStore屬性) 取得的預設傳遞存放區,來識別適當的帳戶。 接著, CreateMeetingRequestFromAccount 會建立 AppointmentItem 。 若要建立專案與帳戶的關聯, CreateMeetingRequestFromAccount 請將Account物件設定為AppointmentItemSendUsingAccount屬性,以將該帳戶指派為專案的傳送帳戶。 指定 SendUsingAccount 屬性是一個重要步驟,因為,除此之外,系統還會針對主要帳戶建立 AppointmentItem 。 在方法的結尾處, CreateMeetingRequestFromAccount 會顯示 AppointmentItem 。 請注意,如果目前的資料夾並非位於傳遞存放區中, 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 支援與意見反應