BeforeViewSwitch Event Example

This example confirms that the user wants to switch views and cancels the switch if the user answers no. The sample code must be placed in a class module, and the Initialize_handler routine must be called before the event procedure can be called by Microsoft Outlook.

Dim myOlApp As Outlook.Application
Public WithEvents myOlExp As Outlook.Explorer

Public Sub Initialize_handler()
    Set myOlExp = myOlApp.ActiveExplorer
End Sub

Private Sub myOlExp_BeforeViewSwitch(ByVal NewView As Variant, Cancel As Boolean)
    Dim Prompt As String
    Prompt = "Are you sure you want to switch to the " & NewView & " view?"
    If MsgBox(Prompt, vbYesNo + vbQuestion) = vbNo Then Cancel = True
End Sub