共用方式為


識別具有帳戶的資料夾

在設定檔中定義多個帳戶的 Microsoft Outlook 會話中,顯示在使用中檔案總管中的資料夾不一定位於該會話的預設存放區;相反地,它可以位於與多個帳戶相關聯的多個存放區之一。 本主題將示範如何識別其預設傳遞存放區與主控該資料夾之存放區相同的帳戶。

在下列程式碼範例中,函 DisplayAccountForCurrentFolder 式會呼叫 GetAccountForFolder 函式來識別預設傳遞存放區裝載目前資料夾的帳戶,然後顯示資料夾的名稱。 GetAccountForFolder會比對從Folder.Store屬性取得的目前資料夾 (存放區,) 每個帳戶的預設傳遞存放區, (使用會話的Accounts 集合中定義的Account.DeliveryStore屬性) 取得。 GetAccountForFolder 找到相符專案時,會傳回 Account 物件;否則,它會傳回 null。

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 元件參考

private void DisplayAccountForCurrentFolder() 
{ 
    Outlook.Folder myFolder = Application.ActiveExplorer().CurrentFolder  
        as Outlook.Folder; 
    string msg = "Account for Current Folder:" + "\n" + 
        GetAccountForFolder(myFolder).DisplayName; 
    MessageBox.Show(msg, 
        "GetAccountForFolder", 
        MessageBoxButtons.OK, 
        MessageBoxIcon.Information); 
} 
 
Outlook.Account GetAccountForFolder(Outlook.Folder folder) 
{ 
    // Obtain the store on which the folder resides. 
    Outlook.Store store = folder.Store; 
 
    // Enumerate the accounts defined for the session. 
    foreach (Outlook.Account account in Application.Session.Accounts) 
    { 
        // Match the DefaultStore.StoreID of the account 
        // with the Store.StoreID for the correct folder. 
        if (account.DeliveryStore.StoreID  == store.StoreID) 
        { 
            // Return the account whose default delivery store 
            // matches the store of the given folder. 
            return account; 
        } 
     } 
     // No account matches, so return null. 
     return null; 
}

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應