Changing CalendarviewMode

Add-in Express™ Support Service
That's what is more important than anything else

Changing CalendarviewMode
 
Mike VE




Posts: 168
Joined: 2007-09-09
I am developing a new version of a calendar add-in for Outlook. It has three ribbon tabs - one to show with CalendarView, one for list views of calendar folders, and one for all other contexts.

They all look alike and have buttons to select views - both the standard day-week-month view and some custom list views. I cannot use the built-in buttons by giving their IdMso as the various built-in calendar view buttons disappear in the other contexts - list views or other folder types.

This should not be a problem - I just build custom buttons using the IdMso of each button as the ImageIdMso. However, I cannot get the code to switch between the CalendarViewModes to work.

Below is a sub that is called when one of the custom calendarView buttons is clicked. The CalendarViewMode is passed in as a parameter. The code looks OK to me but it does nothing. You can see I have added more Apply statements than should be necessary but with or without them the display does not change.

Any idea what I am doing wrong?

Private Sub SetCalendarViewMode(viewMode As Outlook.OlCalendarViewMode)
        Dim olExplorer As Outlook.Explorer = Nothing
        Dim olCurrentFolder As Outlook.MAPIFolder = Nothing
        Dim olViews As Outlook.Views = Nothing
        Dim olView As Outlook.View = Nothing
        Dim olCalendarView As Outlook.CalendarView = Nothing
        Try
            olExplorer = adxModule.OutlookApp.OutlookApplication.ActiveExplorer()
            olCurrentFolder = olExplorer.CurrentFolder
            olViews = olCurrentFolder.Views
            olView = olCurrentFolder.CurrentView
            If olView.ViewType = Outlook.OlViewType.olCalendarView Then
                olCalendarView = olView
                olCalendarView.CalendarViewMode = viewMode
                olCalendarView.Save()
                olCalendarView.Apply()
                olView.Apply()
                olExplorer.Display()
                Application.DoEvents()
            End If
        Catch ex As Exception
            Log.Error(ex, "", "viewMode: " + viewMode.ToString(), messageShownToUser:=False)
            If olCurrentFolder IsNot Nothing Then Marshal.ReleaseComObject(olCurrentFolder)
            If olCalendarView IsNot Nothing Then Marshal.ReleaseComObject(olCalendarView)
            If olViews IsNot Nothing Then Marshal.ReleaseComObject(olViews)
            If olExplorer IsNot Nothing Then Marshal.ReleaseComObject(olExplorer)
        End Try
    End Sub
Posted 23 May, 2018 16:14:01 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Mike,

The code below works for me in Outlook 2016.

Two notes.

1) The OutlookApp property returns the Outlook.Application object that was given to your add-in at startup. Outlook regards this Outlook.Application as "safe". All other Outlook.Application objects aren't safe; using them will end with an Outlook Security warning this day or other. Whence, you should call OutlookApp.ActiveExplorer(), not OutlookApp.OutlookApplication.ActiveExplorer().
2) DoEvents is evil. Ask Google to check if this is so.


Andrei Smolin
Add-in Express Team Leader

    Private Sub AdxRibbonButton1_OnClick(sender As Object, control As IRibbonControl, pressed As Boolean) Handles AdxRibbonButton1.OnClick
        SetCalendarViewMode(Outlook.OlCalendarViewMode.olCalendarViewDay)
    End Sub

    Private Sub SetCalendarViewMode(viewMode As Outlook.OlCalendarViewMode)
        Dim olExplorer As Outlook.Explorer = Nothing
        Dim olCurrentFolder As Outlook.MAPIFolder = Nothing
        Dim olViews As Outlook.Views = Nothing
        Dim olView As Outlook.View = Nothing
        Dim olCalendarView As Outlook.CalendarView = Nothing
        Try
            olExplorer = OutlookApp.ActiveExplorer()
            olCurrentFolder = olExplorer.CurrentFolder
            olViews = olCurrentFolder.Views
            olView = olCurrentFolder.CurrentView
            If olView.ViewType = Outlook.OlViewType.olCalendarView Then
                olCalendarView = olView
                olCalendarView.CalendarViewMode = viewMode
                olCalendarView.Save()
                olCalendarView.Apply()
                'olView.Apply()
                'olExplorer.Display()
                'Application.DoEvents()
            End If
        Catch ex As Exception
            ''Log.Error(ex, "", "viewMode: " + viewMode.ToString(), messageShownToUser:=False)
            If olCurrentFolder IsNot Nothing Then Marshal.ReleaseComObject(olCurrentFolder)
            If olCalendarView IsNot Nothing Then Marshal.ReleaseComObject(olCalendarView)
            If olViews IsNot Nothing Then Marshal.ReleaseComObject(olViews)
            If olExplorer IsNot Nothing Then Marshal.ReleaseComObject(olExplorer)
        End Try
    End Sub
Posted 24 May, 2018 05:29:59 Top
Mike VE




Posts: 168
Joined: 2007-09-09
Hi Andrei

Thanks for your reply. I used adxModule.OutlookApp.OutlookApplication.ActiveExplorer() because I had this sub in a separate module where OutlookApp was out of scope.

Is there a recommended way to get access to OutlookApp in Modules (or C# classes) outside the AddinModule?

I put the sub back in the AddinModule so it was exactly the same as your modified version. I then stepped through every line of the code in debug mode but no change occurred in the display.


Any thoughts?
Posted 24 May, 2018 08:21:18 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Mike VE writes:
Is there a recommended way to get access to OutlookApp in Modules (or C# classes) outside the AddinModule?


{your add-in project e.g. MyAddin1}.AddinModule.CurrentInstance.OutlookApp.

Mike VE writes:
I then stepped through every line of the code in debug mode but no change occurred in the display.


In what event do you invoke this method? Check if it works if called from the Click event of a Ribbon button.


Andrei Smolin
Add-in Express Team Leader
Posted 24 May, 2018 09:08:55 Top
Mike VE




Posts: 168
Joined: 2007-09-09
I invoke it in a button click. The only odd thing is that the sub handles two buttons.

Private Sub AdxRibbonButtonDayView_OnClick(sender As Object, control As IRibbonControl, pressed As Boolean) Handles AdxRibbonButtonDayView.OnClick, AdxRibbonButtonDailyView3.OnClick
        SetCalendarViewMode(Outlook.OlCalendarViewMode.olCalendarViewDay)
End Sub



I too am on Outlook 2016
Posted 24 May, 2018 09:36:51 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Close Outlook and make sure there are no extra OUTLOOK.EXE in the Processes list. What Outlook 2016 build are you using?


Andrei Smolin
Add-in Express Team Leader
Posted 24 May, 2018 09:44:16 Top
Mike VE




Posts: 168
Joined: 2007-09-09
In the process list after I have closed Outlook there is Microsoft Outlook Communications. Outlook is Version 1805 Build 9330.2078.
Posted 25 May, 2018 03:34:56 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Mike,

Please check if http://temp.add-in-express.com/support/MyAddin105-ForMikeVE.zip works for you. Turn all other add-ins off.


Andrei Smolin
Add-in Express Team Leader
Posted 25 May, 2018 04:30:03 Top
Mike VE




Posts: 168
Joined: 2007-09-09
That worked perfectly, Andrei. I added buttons so I had the full range of CalendarViewModes and it worked exactly as expected. So the problem is now to find out why the same code in my solution is not working.

It is a pretty big solution and has been under slow development for about a decade. Some of the early parts of it are not well written. I am gradually improving it as I go along.
Posted 25 May, 2018 05:07:43 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Alas, I can't help you with this as I don't know all the specificities of your solution.

Make sure you don't use the object model and Ribbon components on a background thread. If you use DoEvents calls in your solution, try to comment them out.


Andrei Smolin
Add-in Express Team Leader
Posted 25 May, 2018 07:26:24 Top