It's not hard to image this scenario: you create a custom view, everything is just the way you like it. Then you accidentally sort by a different column. Outlook saves the changes and your perfect view is ruined.
In the early versions of Outlook, users were presented with a "Do you want to save changes?" dialog, however, this dialog was confusing and annoying. Later versions saved changes to view automatically and the scenario above was all too common.
You can use a VBA macro to prevent changes from being saved – you can still sort the columns and change the view, but when you leave the folder and return, the original layout of the custom view is restored.
This VBA sample code will prevent changes from being saved to the views. You can use an If statement to lock only views available to Everyone, or lock all views. If you need to make a change to the view, you'll need to unlock it by changing Call LockView(objView, True) to Call LockView(objView, False).
The macro as, written, locks the view on the default Inbox: Set objViews = objName.GetDefaultFolder(olFolderInbox).Views. You'll need to change the folder to apply it to other folders.
After adding the view to the VBA Editor, click in it then click Run or press F8 key. This will lock the views. You do not need to add a button to the ribbon or toolbar or run it frequently. However, if you need to edit a view, you'll need to unlock the views (by changing True to False) to make the changes then re-lock it.
Macros to lock and unlock "All [Folder type]" views on selected folder and subfolders is in this text file: lock-unlock-views.txt. To use, paste the macros in a module then click in the LocksAllViews or UnlocksAllViews macro and click Run.
Sub LocksPublicViews() 'Locks the interface of all views that are available to 'all users of this folder. Dim objName As Outlook.NameSpace Dim objViews As Outlook.Views Dim objView As Outlook.View Set objName = Application.GetNamespace("MAPI") Set objViews = objName.GetDefaultFolder(olFolderInbox).Views For Each objView In objViews If objView.SaveOption = olViewSaveOptionThisFolderEveryone Then Call LockView(objView, True) ' False = unlock End If Next objView End Sub Sub LockView(ByRef objView As View, ByVal blnAns As Boolean) With objView If blnAns = True Then 'if true lock UI .LockUserChanges = True .Save Else 'if false don't lock UI .LockUserChanges = False End If End With End Sub
The line If objView.SaveOption = olViewSaveOptionThisFolderEveryone Then determines which views are locked. In this example, only the views created for This Folder are locked. This can be changed to lock views available in all folders (of the item type) or lock private views.
Name | Manage Views | New Views Description |
---|---|---|
olViewSaveOptionThisFolderEveryone | This folder | This folder, visible to everyone |
olViewSaveOptionThisFolderOnlyMe | This folder (Private) | This folder, visible only to me |
olViewSaveOptionAllFoldersOfType | All [item type] folders | All [item type] folders |
To lock the view in the current folder, replace
Set objViews = objName.GetDefaultFolder(olFolderInbox).Views with
Set objViews = Application.ActiveExplorer.CurrentFolder.Views
How to use macros
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
Do you know if this works on Outlook 365? I have a user that keeps accidentally dragging their Subject column off of the Inbox and I have to connect remotely to put it back with the Field Chooser. I would just like the different header columns to be locked from being changed. But this VBS doesn't seem to work for me.
Yes, it works in classic Outlook desktop - all versions.
My user complains that her view changes to sorted by size all the time and its driving her mad. I'm wondering from reading the article, does that mean when she does searching, it changes the view?
I'm including the snapshot of her screen
Sorry.. I tried second time and it works like charm !!
Yeah, sometimes it doesn't seem to work the first time.
Great VBA Diane.. Thanks a lot.
I want to lock views of a Search Folder; macro seems not be working on it.
Is it possible by just using
Set objViews = Application.ActiveExplorer.CurrentFolder.Views
As long as it's an 'all users all folders' view, it should work on any folder.