Link to home
Start Free TrialLog in
Avatar of Mike
MikeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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?
Avatar of Rgonzo1971
Rgonzo1971

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
Avatar of Mike

ASKER

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

Regards,

Mike
Have you changed the View1 and View2  to your views?
Avatar of Mike

ASKER

To test it I renamed the view names to View1 and View2 and simply pasted your text in.
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

Avatar of Mike

ASKER

nothing happens I'm afraid.
Avatar of Mike

ASKER

Alexa I think perhaps you have posted an answer to a different question.
EDIT Could you try to exit from OL and re-enter to see if the macros run then
Which macro setting do you have?
ASKER CERTIFIED SOLUTION
Avatar of FarWest
FarWest

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike

ASKER

Thanks FarWest, much appreciated. Works like a charm.

Rgonzo1971 thanks for he;ping.