none
Problems switching between calendar month view and custom list view RRS feed

  • Question

  • In my app I have two ribbon bar buttons intended to make it easy to switch from calendar month view to my created CategoryListView.
    The MonthView button seems to work OK in all circumstances.  The CategoryListView is more problematic.

    In My Calendar module I have a number of Calendar folders in My Calendars and a number in Shared Calendars.

    When I first go to Calendar the last selected calendar is selected in My Calendars and I can use my buttons to switch back and forth between Month and Category view.
    However, if while in Month View I select a different calendar and then click my Category List button nothing happens.  The odd thing is that if I use Outlook Spy after the click the Folder appears to be the newly selected one and the CurrentView is my CategoryList.  It seems that the view has been selected but not properly applied.  Odder still is that if I switch modules by clicking on Mail and then back to Calendar the Category View is now displayed properly.

    What am I missing?

    The sub below is called when the user clicks the Month button

    Private Sub ShowCalendarView()
            'If we are already looking at a calendar then apply the Calendar view to it
            'If not go to the default calendar before applying the view
            Dim olCalView As Outlook.CalendarView
            Try
                If OutlookApp.ActiveExplorer.CurrentFolder.DefaultItemType = Outlook.OlItemType.olAppointmentItem Then
                    OutlookApp.ActiveExplorer.CurrentFolder.Views("Calendar").Apply()
                Else
                    OutlookApp.ActiveExplorer.CurrentFolder = OutlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
                    OutlookApp.ActiveExplorer.CurrentView = "Calendar"
                End If
                olCalView = OutlookApp.ActiveExplorer.CurrentView
                olCalView.CalendarViewMode = Outlook.OlCalendarViewMode.olCalendarViewMonth
            Catch ex As Exception
                oAddin.ErrorMessage(ex, "", "")
            Finally
                If Not olCalView Is Nothing Then Marshal.ReleaseComObject(olCalView)
            End Try
        End Sub
    and here's the one for the CategoryListView
    Private Sub ShowCategoryListView()
    
            If Not AnnualLicencseIsValid() Then Exit Sub
    
            Try
                Select Case NumberOfCalendarsSelectedInNavigationPane()
                    Case 0
                        MessageBox.Show("No calendar is currently selected.")
                    Case 1
                        oCalendar = CalendarSelectedInNavigationPane()
                        If oCalendar.Views(strViewName) Is Nothing Then
                            CreateNewCategoryListView()
                            oCalendar.Views(strViewName).Apply()
                        Else
                            oCalendar.Views(strViewName).Apply()
                        End If
                    Case Else
                        MessageBox.Show(String.Format("You have more than one calendar selected.{0}{0}The List by Category view only works for a single calendar at a time.{0}{0}Select just one calendar and try again.", vbCrLf))
                End Select
            Catch ex As Exception
                oAddin.ErrorMessage(ex, "", "")
            End Try
            Application.DoEvents()
        End Sub
    and here is how the list view is created.
    Private Sub CreateNewCategoryListView()
            Dim currentViews As Outlook.Views
            Dim categoryView As Outlook.View
            Dim categoryTableView As Outlook.TableView
            Try
                currentViews = OutlookApp.ActiveExplorer.CurrentFolder.Views
                currentViews.Add(strViewName, Outlook.OlViewType.olTableView, Outlook.OlViewSaveOption.olViewSaveOptionAllFoldersOfType)
    
                'Get a reference to the newly added table view and adjust its properties
                categoryView = currentViews(strViewName)
                categoryTableView = categoryView
                categoryTableView.AutoPreview = Outlook.OlAutoPreview.olAutoPreviewNone
                categoryTableView.AllowInCellEditing = False
                categoryTableView.SortFields.RemoveAll()
                categoryTableView.SortFields.Add("Categories")
                categoryTableView.ShowItemsInGroups = True
                categoryTableView.AutomaticGrouping = True
                categoryTableView.ViewFields.Remove("Recurrence")
                categoryTableView.DefaultExpandCollapseSetting = Outlook.OlDefaultExpandCollapseSetting.olAllCollapsed
                categoryTableView.Save()
                categoryTableView.Apply()
            Catch ex As Exception
                oAddin.ErrorMessage(ex, "", "")
            Finally
                If Not categoryTableView Is Nothing Then Marshal.ReleaseComObject(categoryTableView)
                If Not categoryView Is Nothing Then Marshal.ReleaseComObject(categoryView)
                If Not currentViews Is Nothing Then Marshal.ReleaseComObject(currentViews)
            End Try
        End Sub
    I'd be grateful for any pointers.


    Mike VE

    Saturday, November 10, 2012 5:25 PM

All replies

  • Hi Mike,

    Thanks for posting in the MSDN Forum.

    I've tried to reproduce your issue on following snippet:

    Imports Microsoft.Office.Tools.Ribbon
    Imports System.Runtime.InteropServices
    Imports System.Diagnostics
    
    Public Class Ribbon1
    
        Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
            Dim olApp As Outlook.Application = Globals.ThisAddIn.Application
            Dim olCalendar As Outlook.MAPIFolder = olApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
    
            If olCalendar.Views("TestView20121112") IsNot Nothing Then
                CreateNewCategoryListView()
                olCalendar.Views("TestView20121112").Apply()
            End If
    
        End Sub
    
    
        Private Sub CreateNewCategoryListView()
            Dim currentViews As Outlook.Views
            Dim categoryView As Outlook.View
            Dim categoryTableView As Outlook.TableView
            Dim OutlookApp As Outlook.Application = Globals.ThisAddIn.Application
            Dim strViewName As String = "TestView20121112"
            Try
                currentViews = OutlookApp.ActiveExplorer.CurrentFolder.Views
                If currentViews(strViewName) IsNot Nothing Then
                    currentViews.Remove(strViewName)
                End If
                currentViews.Add(strViewName, Outlook.OlViewType.olTableView, Outlook.OlViewSaveOption.olViewSaveOptionAllFoldersOfType)
    
                'Get a reference to the newly added table view and adjust its properties
                categoryView = currentViews(strViewName)
                categoryTableView = categoryView
                categoryTableView.AutoPreview = Outlook.OlAutoPreview.olAutoPreviewNone
                categoryTableView.AllowInCellEditing = False
                categoryTableView.SortFields.RemoveAll()
                categoryTableView.SortFields.Add("Categories")
                categoryTableView.ShowItemsInGroups = True
                categoryTableView.AutomaticGrouping = True
                categoryTableView.ViewFields.Remove("Recurrence")
                categoryTableView.DefaultExpandCollapseSetting = Outlook.OlDefaultExpandCollapseSetting.olAllCollapsed
                categoryTableView.Save()
                categoryTableView.Apply()
            Catch ex As Exception
                Trace.TraceInformation(ex.Message)
            Finally
                If Not categoryTableView Is Nothing Then Marshal.ReleaseComObject(categoryTableView)
                If Not categoryView Is Nothing Then Marshal.ReleaseComObject(categoryView)
                If Not currentViews Is Nothing Then Marshal.ReleaseComObject(currentViews)
            End Try
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button2.Click
            CreateNewCategoryListView()
        End Sub
    
    
        Private Sub Button3_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button3.Click
            Dim oCalendar As Outlook.MAPIFolder = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
    
            oCalendar.Views("Calendar").Apply()
        End Sub
    End Class
    

    All work fine on my side.

    I'm wondering whether oCalendar hasn't access the instance from CalendarSelectedInNavigationPane method.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 12, 2012 5:29 AM
    Moderator
  • Hi Tom

    Thanks for the response.  I made a new project with your code.  It worked fine on the default calendar but not when I switched folders.

    I added some code in the ExplorerFolderSwitch event to re-assign oCalendar to the current folder.

    With this in place your code worked fine on Calendars in MyCalendar.

    However, it did not work for a Shared folder.  If you select a shared folder a folder in MyCalendars remains selected so you have to de-select that to leave just the shared calendar selected.  If you then click your Button1 nothing happens although OutlookSpy confirms that the selected folder is a shared folder (it has a great long guid where a normal folder has FolderPath) and the CurrentView.Name is TestView20121112.

    I can go to the View > Change view > TestView20121112 and that works OK.

    So how can I get my code to apply the view to a shared calendar?


    Mike VE

    Monday, November 12, 2012 11:24 AM