Outlook Macro to Change Views

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?
Mike235Asked:
Who is Participating?
 
FarWestCommented:
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

0
 
Rgonzo1971Commented:
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
0
 
Mike235Author Commented:
Hi - unfortunately this does not seem to work. Running the macro has no visible effect.

Regards,

Mike
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Rgonzo1971Commented:
Have you changed the View1 and View2  to your views?
0
 
Mike235Author Commented:
To test it I renamed the view names to View1 and View2 and simply pasted your text in.
0
 
Rgonzo1971Commented:
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

0
 
Mike235Author Commented:
nothing happens I'm afraid.
0
 
Mike235Author Commented:
Alexa I think perhaps you have posted an answer to a different question.
0
 
Rgonzo1971Commented:
EDIT Could you try to exit from OL and re-enter to see if the macros run then
0
 
Rgonzo1971Commented:
Which macro setting do you have?
0
 
Mike235Author Commented:
Thanks FarWest, much appreciated. Works like a charm.

Rgonzo1971 thanks for he;ping.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.