取得資料夾的帳戶
這個範例會取得與目前會話中資料夾相關聯的帳戶。
範例
在下列程式代碼範例中,DisplayAccountForCurrentFolder 函式會呼叫 GetAccountForFolder 函式來識別預設傳遞存放區裝載目前資料夾的帳戶,然後顯示資料夾的名稱。 GetAccountForFolder 會比對從 Store 屬性取得的目前資料夾 (存放區,) 每個帳戶的預設傳遞存放區, (使用會話的 Accounts 集合中定義的 DeliveryStore 屬性) 取得。 如果找到相符專案,GetAccountForFolder 會傳回 Account 物件;否則,它會傳回 Null 參考。
在配置檔中定義多個帳戶的 Microsoft Outlook 會話中,顯示在使用中檔案總管中的資料夾不一定位於該工作階段的預設存放區;相反地,它可以位於與多個帳戶相關聯的多個存放區之一。 本主題將示範如何識別其預設傳遞存放區與主控該資料夾之存放區相同的帳戶。
If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.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 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 currect 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;
}