共用方式為


將資料夾新增至我的最愛資料夾群組

You can add a folder to the Favorite Folders navigation group in Microsoft Outlook by using the Add method of the NavigationFolders collection for a NavigationGroup object. Add方法會接受Folder物件參考,而自訂導覽資料夾會與該物件相關聯。

您可以使用 NavigationGroups 物件的 GetDefaultNavigationGroup 方法,擷取指定導覽群組類型之預設流覽群組的 NavigationGroup 物件參照。

此範例會針對重要項目建立新的郵件資料夾,並且在 [郵件] 模組的 [我的最愛資料夾] 巡覽群組中,針對新的資料夾新增自訂的巡覽資料夾。

注意 如果您嘗試將針對 [解決方案] 模組建立的解決方案特定資料夾新增至 [我的最愛資料夾] 清單,Outlook 將會引發錯誤。

The sample performs the following actions:

  1. 本範例會先使用 NameSpace 物件的 GetDefaultFolder 方法,取得目前使用者之 [收件匣] 預設資料夾的 Folder 物件參照。

  2. 然後,會在 [收件匣] 預設資料夾的 Folders 集合中,建立代表新的郵件資料夾、名稱為 "Important Items" 的新 Folder 物件。

  3. 本範例接著會取得對作用中瀏覽器之 NavigationPane 物件的參照,並且使用 NavigationModules 集合的 GetNavigationModule 方法來取得 MailModule 物件參照。

  4. 然後,它會使用MailModuleNavigationGroups集合的GetDefaultNavigationGroup方法,取得 [我的最愛資料夾] 流覽群組的NavigationGroup物件參照。

  5. 最後,本範例會使用巡覽群組之 NavigationGroups 集合的 Add 方法,根據其先前所建的 Folder 物件,將 NavigationFolder 物件新增至巡覽群組中。

Private Sub CreateImportantFavoritesFolder() 
    Dim objNamespace As NameSpace 
    Dim objInbox As Folder 
    Dim objFolder As Folder 
     
    Dim objPane As NavigationPane 
    Dim objModule As MailModule 
    Dim objGroup As NavigationGroup 
    Dim objNavFolder As NavigationFolder 
     
    On Error GoTo ErrRoutine 
     
    ' First, retrieve the default Inbox folder. 
    Set objNamespace = Application.GetNamespace("MAPI") 
    Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox) 
     
    ' Create a new mail folder named "Important Items". 
    Set objFolder = objInbox.Folders.Add("Important Items") 
         
    ' Get the NavigationPane object for the 
    ' currently displayed Explorer object. 
    Set objPane = Application.ActiveExplorer.NavigationPane 
     
    ' Get the mail module from the Navigation Pane. 
    Set objModule = objPane.Modules.GetNavigationModule(olModuleMail) 
     
    ' Get the "Favorite Folders" navigation group from the 
    ' mail module. 
    With objModule.NavigationGroups 
        Set objGroup = .GetDefaultNavigationGroup(olFavoriteFoldersGroup) 
    End With 
     
    ' Add a new navigation folder for the "Important Items" 
    ' folder in the "Favorite Folders" navigation group. 
    Set objNavFolder = objGroup.NavigationFolders.Add(objFolder) 
     
EndRoutine: 
    On Error GoTo 0 
    Set objNavFolder = Nothing 
    Set objFolder = Nothing 
    Set objGroup = Nothing 
    Set objModule = Nothing 
    Set objPane = Nothing 
    Set objNamespace = Nothing 
    Exit Sub 
 
ErrRoutine: 
    MsgBox Err.Number & " - " & Err.Description, _ 
        vbOKOnly Or vbCritical, _ 
        "CreateImportantFavoritesFolder" 
End Sub

支援和意見反應

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