共用方式為


根據資料夾路徑取得資料夾

此範例會採用資料夾路徑,並取得相關聯的資料夾。

範例

注意事項

下列程式代碼範例是 Microsoft Office Outlook 2007 程式設計應用程式的摘錄。

在下列程式代碼範例中,GetKeyContacts 方法會使用 GetRootFolder () 屬性來取得 Contacts\Key Contacts 資料夾路徑。 接著會使用 FolderPath 屬性做為自變數來呼叫 GetFolder 方法。 如果 GetFolder 傳回資料夾,就會出現一則訊息,指出找到密鑰聯繫人。 GetFolder 方法會採用資料夾的路徑,並傳回正確的 Folder 物件。 這會先將 FolderPath 屬性分割成字串數位,然後使用 數位來尋找從 FolderPath 屬性頂端開始的正確 Folder 物件。 如果找不到指定的資料夾,GetFolder 會傳回 Null 參考。

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 GetKeyContacts()
{
    string folderPath =
        Application.Session.
        DefaultStore.GetRootFolder().FolderPath
        + @"\Contacts\Key Contacts";
    Outlook.Folder folder = GetFolder(folderPath);
    if (folder != null)
    {
        //Work with folder here
        Debug.WriteLine("Found Key Contacts");
    }
}

// Returns Folder object based on folder path
private Outlook.Folder GetFolder(string folderPath)
{
    Outlook.Folder folder;
    string backslash = @"\";
    try
    {
        if (folderPath.StartsWith(@"\\"))
        {
            folderPath = folderPath.Remove(0, 2);
        }
        String[] folders =
            folderPath.Split(backslash.ToCharArray());
        folder =
            Application.Session.Folders[folders[0]]
            as Outlook.Folder;
        if (folder != null)
        {
            for (int i = 1; i <= folders.GetUpperBound(0); i++)
            {
                Outlook.Folders subFolders = folder.Folders;
                folder = subFolders[folders[i]]
                    as Outlook.Folder;
                if (folder == null)
                {
                    return null;
                }
            }
        }
        return folder;
    }
    catch { return null; }
}        

另請參閱