none
Change CurrentView in Outlook vsto RRS feed

  • Question

  • Hello everybody,

    For a functionality I want to add a button to specific received mails.

    Without going in too much detail I retrieve an AppointmentItem from the mail.

    Now I want to add a button to jump to the Appointment in the Calendar.

    This is causing me some problems.

    I've tried changing the Globals.ThisAddIn.Application.ActiveExplorer().CurrentView, but it doesn't seem to work.

    The biggest problem seems to be to change the CurrentView.ViewType, because it is read-only.

    So I was thinking maybe I have to create a new View and the update the CurrentView, but that doesn't seem to work either.

    I hope someone here can point me in the right direction.

    Thanks in advance,

    Frenk

    Monday, July 9, 2018 11:46 AM

Answers

  • To properly reset the current view, you must do a View.Reset and then a View.Apply . 

    Anyway, to change the current view you need to run the following code:

    Sub ChangeCurrentView() 
     Dim myOlExp As Outlook.Explorer 
     Set myOlExp = Application.ActiveExplorer 
     If myOlExp.CurrentFolder = "Inbox" Then 
       myOlExp.CurrentView = "Messages" 
     End If 
    End Sub

    Hope it helps.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by IAmFrenk Tuesday, July 10, 2018 2:20 PM
    Tuesday, July 10, 2018 12:53 PM

All replies

  • I feel like I've gotten somewhat closer to the solution, but I still don't have it working completely.

    From what I've gathered online, I have to set the ActiveExplorer's CurrentFolder AND the ActiveExplorer's CurrentView.

    Getting and setting the CurrentFolder isn't an issue, because I have a specific folder where the AppointmentItem is in.

    But getting the CurrentView is a issue, because I just don't get how te retrieve (or create?) it.

    Maybe someone here can help me?

    Thanks in advance.

    Monday, July 9, 2018 1:31 PM
  • Hello Frenk,

    To obtain a View object for the view of the current Explorer , use Explorer.CurrentView instead of the CurrentView property of the current Folder object returned by Explorer.CurrentFolder .

    You must save a reference to the View object returned by CurrentView before you proceed to use it for any purpose.

    The View object allows you to create customizable views that allow you to better sort, group and ultimately view data of all different types. There are a variety of different view types that provide the flexibility needed to create and maintain your important data.

    • The table view type ( olTableView ) allows you to view data in a simple field-based table.

    • The Calendar view type ( olCalendarView ) allows you to view data in a calendar format.

    • The card view type ( olCardView ) allows you to view data in a series of cards. Each card displays the information contained by the item and can be sorted.

    • The icon view type ( olIconView ) allows you to view data as icons, similar to a Windows folder or explorer.

    • The timeline view type ( olTimelineView ) allows you to view data as it is received in a customizable linear time line.

    Views are defined and customized using the View object's XML property. The XML property allows you to create and set a customized XML schema that defines the various features of a view.

    Use Views ( index ), where index is the name of the View object or its ordinal value, to return a single View object.

    Use the Add method of the Views collection to create a new view. Always use Save to save a view after you change any property of the view.

    The XML definition describes the view type by using a series of tags and keywords corresponding to various properties of the view itself. When the view is created, the XML definition is parsed to render the settings for the new view.

    To determine how the XML should be structured when creating views, you can create a view by using the Outlook user interface and then you can retrieve the XML property for that view.

    To programmatically add a custom field to a view, use the Add method of the ViewFields object. This is the recommended way to dynamically change the view over setting the XML property of the View object.

    Sub DisplayViewDef() 
     'Displays the XML definition of a View object 
     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 
     'Return a view called Table View if it already exists, else create one 
     Set objView = objViews.Item("Table View") 
     If objView Is Nothing Then 
     Set objView = objViews.Add("Table View", olTableView, olViewSaveOptionAllFoldersOfType) 
     End If 
     MsgBox objView.XML 
    End Sub


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, July 9, 2018 9:14 PM
  • Hi Eugene,

    Thanks for your reply, I feel like I understand Views better now.

    Regrettably I still can't get the View to switch from a mail to my custom calendar folder.

    Maybe you see what I do wrong here:

    private static void ChangeCurrentViewToCustomCalendar()
    {
    	try
    	{
    		Outlook.CalendarView calendarView = null;
    		var customCalendarFolder = GetCustomCalendarFolder(); // This returns an MAPFolder object of my custom calendar-folder.
    		Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder = customCalendarFolder;
    		var views = customCalendarFolder.Views;
    
    		var foundCalendarView = false;
    		foreach (Outlook.View view in views)
    		{
    			if (view is Outlook.CalendarView)
    			{
    				calendarView = view as Outlook.CalendarView;
    				foundCalendarView = true;
    				break;
    			}
    		}
    
    		if (!foundCalendarView)
    		{
    			// Create and save new CalendarView.
    		}
    
    		Globals.ThisAddIn.Application.ActiveExplorer().CurrentView = calendarView;
    		calendarView.Apply();
    		calendarView.CalendarViewMode = Outlook.OlCalendarViewMode.olCalendarViewMonth;
    		calendarView.GoToDate(DateTime.Now);
    	}
    	catch (Exception ex)
    	{
    		// Logger
    	}
    }


    Thanks in advance!

    Tuesday, July 10, 2018 9:30 AM
  • Maybe helpful if I tell what happens with my current code.

    I have a button that calls the above function. The button is inside a FormRegion that only shows up for specific mails.

    After I press the button the left side of Outlook show the right custom calendar folder, but the mail disappears. After some seconds Outlook closes without an error message or anything.

    Screenshot right before Outlook closes:

    Tuesday, July 10, 2018 9:45 AM
  • To properly reset the current view, you must do a View.Reset and then a View.Apply . 

    Anyway, to change the current view you need to run the following code:

    Sub ChangeCurrentView() 
     Dim myOlExp As Outlook.Explorer 
     Set myOlExp = Application.ActiveExplorer 
     If myOlExp.CurrentFolder = "Inbox" Then 
       myOlExp.CurrentView = "Messages" 
     End If 
    End Sub

    Hope it helps.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by IAmFrenk Tuesday, July 10, 2018 2:20 PM
    Tuesday, July 10, 2018 12:53 PM