Outlook Macro to Change Views

Mike
Mike used Ask the Experts™
on
Hi - I need to write a macro in Outlook 2010 to toggle between two saved Views, as follows:

Assume the saved Views are called "View 1" and "View 2". To change from the current View to "View 1" manually I would type:

Click Tab 'View' > Click "Change View" > Click "View 1" from the list of saved views

I want a macro which has the following effect:
Case 1: If View 1 is current view the macro should change the view to View 2
Case 2: If View 2 is current view the macro should change the view to View 1
Case 3: if neither View 1 nor View 2 is the current view the macro should change the view to View 1

Can anyone assist with this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
HI,

pls try something like this
Sub Macro()
    Set objName = Application.GetNamespace("MAPI")
    Set objViews = objName.GetDefaultFolder(olFolderInbox).Views
    Set ActiveView = Application.ActiveExplorer.CurrentView
    If ActiveView.Name = "View1" Then
        objViews("View2").Apply
    Else
        objViews("View1").Apply
    End If

End Sub

Open in new window

Regards
MikeCEO

Author

Commented:
Hi - unfortunately this does not seem to work. Running the macro has no visible effect.

Regards,

Mike
Top Expert 2016

Commented:
Have you changed the View1 and View2  to your views?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

MikeCEO

Author

Commented:
To test it I renamed the view names to View1 and View2 and simply pasted your text in.
Top Expert 2016

Commented:
Hi could you try this to see if the macros run

Sub Macro1()
    Set objName = Application.GetNamespace("MAPI")
    Set objviews = objName.GetDefaultFolder(olFolderInbox).Views
    For Each vw In objviews
        Debug.Print vw.Name
    Next
End Sub

Open in new window

MikeCEO

Author

Commented:
nothing happens I'm afraid.
MikeCEO

Author

Commented:
Alexa I think perhaps you have posted an answer to a different question.
Top Expert 2016

Commented:
EDIT Could you try to exit from OL and re-enter to see if the macros run then
Top Expert 2016

Commented:
Which macro setting do you have?
Commented:
try this
Sub Macro()
    If Application.ActiveExplorer.CurrentView.Name = "View1" Then
       Application.ActiveExplorer.CurrentView = "View2"
    Else
        Application.ActiveExplorer.CurrentView = "View1"
    End If

End Sub
                                          

Open in new window

MikeCEO

Author

Commented:
Thanks FarWest, much appreciated. Works like a charm.

Rgonzo1971 thanks for he;ping.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial