In the thread at OL10 add views to 'my tasks'> navigation pane, users are disappointed the list of views was removed from the Navigation pane in Outlook 2010. While this macro won't add the views list back, it can make it easier to switch between your favorite views.
You'll need to create one macro and one button on the QAT or ribbon for each view you want to use, so it's not practical to use with a large number of views, but will be useful for a few of your most-used views.
If you add the Change View command to the QAT (Quick Access Toolbar) accessing the lesser-used views will be a couple of steps less than using the View ribbon.
To use the macro, press Alt+F11 to open the VBA editor. Right click on Project1 and choose Insert > Module. Paste the macro into the new module, one for each button you want to create.
Make the following changes to each macro:
- Change the macro name (Sub GetAssignedView()) so it is unique for each macro. I use the view name in the macro name.
- Change the name of the view in each macro (Set objView = objViews.Item("Assigned")) to match the name of the desired view.
- While Set objViews = Application.ActiveExplorer.CurrentFolder.Views allows the macro to work with the folder you are viewing, the view needs to be available for the selected folder. If it's not listed in Change Views when you are viewing the folder, it's not available.
Don't forget to check your macro security settings!
After creating your macros:
- Go to File, Options, Customize Ribbon or Quick Access Toolbar
- In Choose commands from select Macros
- Select your macro and click Add
- Repeat for each macro
Note: If you are adding the views to a ribbon, you need to add a New Group to either your most used tab or add a New Tab to add the group to.
Sub GetAssignedView() Dim objViews As Views Dim objView As View ' Applies view to select folder Set objViews = Application.ActiveExplorer.CurrentFolder.Views 'Get the view Set objView = objViews.Item("Assigned") 'apply the view objView.Apply End Sub
Assign View to Folder and Subfolders
This macro applies the view to the selected folder and it's subfolders (but not nested subfolders), after first checking to see if the folder is the correct type (Contacts in this example.)
To use, select either the parent folder and run the macro.
Sub SetViewSubFolders() Dim CurrentFolder As Outlook.folder Dim i As Long Dim objViews As Views Dim objView As View ' Applies view to select folder Set CurrentFolder = Application.ActiveExplorer.CurrentFolder Set objViews = CurrentFolder.Views 'Get the view Set objView = objViews.Item("Assigned") 'apply the view objView.Apply For i = CurrentFolder.Folders.Count To 1 Step -1 Set olNewFolder = CurrentFolder.Folders(i) For Each olNewFolder In CurrentFolder.Folders ' use if to apply to specific folder type If olNewFolder.DefaultItemType = olContactItem Then Debug.Print olNewFolder.Name Set objViews = olNewFolder.Views Set objView = objViews.Item("Assigned") objView.Apply End If Next Next i End Sub
Folder Type | DefaultItemType |
---|---|
Calendar | olAppointmentItem |
Contacts | olContactItem |
Journal | olJournalItem |
olMailItem | |
Notes | olNoteItem |
Tasks | olTaskItem |
Apply a View to Nested Subfolders
This macro is based off of Michael Bauer's Folder: Expand all Folders macro and works on nested folders. The view is assigned in the LoopFolders macro.
Public Sub AssignViewtoFolders() On Error Resume Next Dim Ns As Outlook.NameSpace Dim Folders As Outlook.Folders Dim CurrF As Outlook.MAPIFolder Dim F As Outlook.MAPIFolder Dim ExpandDefaultStoreOnly As Boolean Set Ns = Application.GetNamespace("Mapi") Set CurrF = Application.ActiveExplorer.CurrentFolder Set F = Ns.GetDefaultFolder(olFolderInbox) Set F = F.Parent Set Folders = F.Folders LoopFolders Folders, True 'DoEvents Set Application.ActiveExplorer.CurrentFolder = CurrF End Sub Private Sub LoopFolders(Folders As Outlook.Folders, _ ByVal bRecursive As Boolean _ ) Dim F As Outlook.MAPIFolder For Each F In Folders Set Application.ActiveExplorer.CurrentFolder = F If F.DefaultItemType = olContactItem Then Debug.Print F.Name Set objViews = F.Views Set objView = objViews.Item("Assigned") objView.Apply End If If bRecursive Then If F.Folders.Count Then LoopFolders F.Folders, bRecursive End If End If Next End Sub
Use the Macro with Multiple Views
To use this code with multiple views, you can create a public string and use a macro to assign a View name to the string then call the assign view macro.
Like this:
Public strView As String Sub NewView() strView = "New View" GetAssignedView End Sub Sub View1() strView = "View1" GetAssignedView End Sub Sub GetAssignedView() Dim objViews As Views Dim objView As View ' Applies view to select folder Set objViews = Application.ActiveExplorer.CurrentFolder.Views 'Get the view Set objView = objViews.Item(strView) 'apply the view objView.Apply End Sub
Open the Calendar pane to a specific Calendar and view
Use this code to open a specific calendar using a specific view every time you switch to the calendar module.
To use: paste into ThisOutlookSession, change the name of the view (it's case sensitive), then click in Application_Startup and click Run. Switch navigation pane folders to test.
Dim WithEvents objPane As NavigationPane Private Sub Application_Startup() Set objPane = Application.ActiveExplorer.NavigationPane End Sub Private Sub objPane_ModuleSwitch(ByVal CurrentModule As NavigationModule) Dim objModule As CalendarModule Dim objGroup As NavigationGroup Dim objNavFolder As NavigationFolder If CurrentModule.NavigationModuleType = olModuleCalendar Then Set objModule = objPane.Modules.GetNavigationModule(olModuleCalendar) Set objGroup = objModule.NavigationGroups("My Calendars") '' Change the 2 to start in a different folder Set objNavFolder = objGroup.NavigationFolders.Item(2) objNavFolder.IsSelected = True Dim objViews As Views Dim objView As View Set objViews = Application.ActiveExplorer.CurrentFolder.Views Set objView = objViews.Item("New view 4") objView.Apply End If Set objNavFolder = Nothing Set objGroup = Nothing Set objModule = Nothing End Sub
Thanks! This works well.
I now have a "WORKING" View showing only categorised emails. I can work in here easily without new emails popping in and breaking my concentration. I also have a "PROCESS NEW" View that shows uncategorised emails. Any new ones or ones that haven't been processed yet show up here.
While it was helpful I found your article confusingly written. Example 1: Before you show any of the code for the macro you say, "Make the following changes to each macro" and then list changes that need to be made. Without having first seen the macro without the changes, there is no context. I find with instructions for anything it is far better to show what "X" is before telling the reader, "Change X as follows". Example 2: You write "Change the macro name (Sub GetAssignedView()) so it is unique for each macro. I use the view name in the macro name." What is the macro name? Is it the word "Sub" or "GetAssignedView" or ??? What text in that name am I supposed to change? Is the view name supposed to be inserted in the inner set of parentheses? Or should the text "GetAssignedView" be changed for the view name? Or ??? Example 3: You write, "Change the name of the view in each macro (Set objView = objViews.Item("Assigned")) to match the name of the desired view." Again, what text in that parenthetical should be changed? Should I just change "Assigned" to be the name of the desired view… Read more »
Hi
Is it possible to read all exiting vievs for contacts, open a box with the list to click on the view to show it up?
To the best of my knowledge, no, not using VBA.
aloha Diane.. i am using the vba code to expand all the mail folders. It works perfectly on my PC, but when i try to apply the same to another PC, it fails. i remarked the "on error resume next" and a pop up shows the operation failed. when i debug, it highlights the following line. Set Application.ActiveExplorer.CurrentFolder = F the only difference between the 2 PC's is one has a microsoft exchange account. what am i missing? and thank you for your time and sharing your insight....i really appreciate it!!! Sub ExpandFolders() ' On Error Resume Next Dim Ns As Outlook.NameSpace Dim Folders As Outlook.Folders Dim CurrF As Outlook.MAPIFolder Dim F As Outlook.MAPIFolder Dim ExpandDefaultStoreOnly As Boolean ExpandDefaultStoreOnly = False Set Ns = Application.GetNamespace("Mapi") Set CurrF = Application.ActiveExplorer.CurrentFolder If ExpandDefaultStoreOnly = True Then Set F = Ns.GetDefaultFolder(olFolderInbox) Set F = F.Parent Set Folders = F.Folders LoopFolders Folders, True Else LoopFolders Ns.Folders, True End If DoEvents Set Application.ActiveExplorer.CurrentFolder = CurrF End Sub Private Sub LoopFolders(Folders As Outlook.Folders, _ ByVal bRecursive As Boolean _ ) Dim F As Outlook.MAPIFolder For Each F In Folders Set Application.ActiveExplorer.CurrentFolder = F DoEvents If bRecursive Then If F.Folders.Count Then LoopFolders F.Folders, bRecursive End If End… Read more »
Hi Diane,
I am trying to come up with a Macro that specifically expands the In-Place Archive (Online Archive) and sub-folders on startup. Is there any code to accomplish this?
Thanks!
See if the macro here works with the archive folders - https://www.vboffice.net/en/developers/expand-all-folders/
I tend to prefer keeping my hands on the keyboard, so Alt+F1 followed by Ctrl+F1 is probably going to work best for me... with about the same effort as the reading button. Thanks for the suggestion though!
Hi Diane,
I have two basic modes in which I use Outlook - managing email and reading email. I like to set my Outlook view to best accommodate each mode. When I'm reading emails the Ribbon and Navigation Pane should both be hidden and unhidden all other times.
For quickly switching between my two views, I'd simply like to combine the functionality of Ctrl+F1 (Hide Ribbon) with the functionality of Alt+F1 (Hide Navigation Pane) into a single action.
Can this be done through a short macro assigned to a hotkey combination?
No, it can't be automated... however have you tried the reading buttons in the status bar? It might not get you the exact format you want (the ribbon isn't affected) but should come close. It's the buttons in the status bar on the far right, next to zoom.
Hi,
I would like the macro to apply a specific view to all sub-folders in my inbox.
thanks,
Phil
I updated the article to include a macro to apply a view to the selected folder and subfolders.
https://www.slipstick.com/developer/apply-view-to-folder-using-vba/#subfolders