Search code examples
.netoutlookvstoexchange-serveroutlook-addin

VSTO: Cached exchange mode VS LastModificationTime


I am developing a VSTO Outlook Add-In that is relying on the LastModificationTime property of Outlook Appointment. The problem is when Cached exchange mode is turned On, the LastModificationTime property auto updates each time I close Outlook. Is there possible solution I can use to get the date and time when user changed the appointment, instead of date and time when cached exchange mode changed the appointment?

Seeing that there are not a lot of responses I wanted to describe my problem in more detail - this is what happens:

  1. I change an item (the abnormal behavior happens only to items I've changed)
  2. LastModificationTime is changed to the time when I've saved the item (I see the change with OutlookSpy). (eg. LastModificationTime 3:30:00 PM)
  3. I work until 4:00:00 PM and check the LastModificationTime and it still shows 3:30:00 PM
  4. I close outlook
  5. I open outlook and check LastModificationTime. Now the LastModificationTime shows 3:30:42 instead of 3:30:00. Why did it add extra 42 seconds after I had reopened the Outlook?

Thank you for any suggestions you can give me.


Solution

  • I was able to find two workarounds for my problem, #1 being unacceptable for me and #2 I actually used:

    Solution #1: Use registry entries to disable exchange server on add-in shutdown and re-enable it on add-in startup. Below is sample code:

        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        Try
            Dim regTopKey As String = "HKEY_CURRENT_USER"
            Dim regPath As String = "\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\13dbb0c8aa05101a9bb000aa002fc45a"
            Dim oldValue As Byte() = Registry.GetValue(regTopKey & regPath, "00036601_Backup", Nothing)
            If oldValue IsNot Nothing Then
                Registry.SetValue(regTopKey & regPath, "00036601", oldValue, RegistryValueKind.Binary)
            End If
        Catch
        End Try
    End Sub
    
    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
        Try
            Dim disableExchangeMode As Byte() = {4, 0, 0, 0}
            Dim regTopKey As String = "HKEY_CURRENT_USER"
            Dim regPath As String = "\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\13dbb0c8aa05101a9bb000aa002fc45a"
            Dim currentValue As Byte() = Registry.GetValue(regTopKey & regPath, "00036601", Nothing)
            If currentValue IsNot Nothing Then
                Registry.SetValue(regTopKey & regPath, "00036601_Backup", currentValue, RegistryValueKind.Binary)
            End If
            Registry.SetValue(regTopKey & regPath, "00036601", disableExchangeMode, RegistryValueKind.Binary)
        Catch
        End Try
    End Sub
    

    Solution #2: Detect when user changes an appointment item and save the change in user defined property field. Below is sample code:

    Private Sub appointmentSave(ByVal Item As Object) Handles _m_olAppointment.ItemChange, _m_olAppointment.ItemAdd
        Try
            Dim dateNow As Date = Date.Now
            If TypeOf Item Is Outlook.AppointmentItem Then
                If (dateNow - _lastFolderSwitch).TotalMilliseconds > 500 Then
                    _lastFolderSwitch = dateNow
                    Dim appointmentItem As Outlook.AppointmentItem = CType(Item, Outlook.AppointmentItem)
                    If (dateNow - appointmentItem.LastModificationTime).TotalMilliseconds < 100 Then
                        Dim lastModifiedDate As Outlook.UserProperty = appointmentItem.UserProperties.Add("lastModifiedDate", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, True)
                        lastModifiedDate.Value = dateNow.ToString
                        appointmentItem.Save()
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    

    Thank You everybody that helped