如何检测Outlook今日视图(Today View)是否打开或切换
Great question! Unfortunately, the Outlook object model doesn’t expose a direct "IsTodayViewActive" property, but there are reliable workarounds to detect when the Today View is active—whether on Outlook startup or when the user switches views. Here’s how to implement this for your add-in:
1. Core Detection Logic
The Today View is a specialized CalendarView that only displays the current day. To identify it reliably (avoiding localized view name issues like "Today" vs. "今日"), you can:
- Verify the current folder is a calendar folder
- Check if the active view is a
CalendarView - Confirm the view’s date range exactly matches the current day
Here’s a helper method to handle this:
private bool IsTodayView(Outlook.Explorer explorer) { // Skip if we're not in a calendar folder if (explorer.CurrentFolder.DefaultItemType != Outlook.OlItemType.olAppointmentItem) return false; // Cast to CalendarView (Today View is always a CalendarView) if (explorer.CurrentView is Outlook.CalendarView calendarView) { DateTime today = DateTime.Today; // Check if the view's range starts at midnight today and ends at midnight tomorrow return calendarView.Start.Date == today && calendarView.End.Date == today.AddDays(1); } return false; }
2. Capture View Switch & Startup Events
You need to monitor two critical scenarios:
- Outlook Startup: Check the initial active view when the add-in loads
- View Switch: Detect when the user navigates to (or away from) the Today View
Add this code to your ThisAddIn class:
private void ThisAddIn_Startup(object sender, EventArgs e) { // Handle the initial active explorer window if (Application.ActiveExplorer() != null) { Application.ActiveExplorer().ViewSwitch += OnViewSwitch; UpdateRibbonButtons(Application.ActiveExplorer()); } // Monitor new explorer windows (e.g., user opens a second Outlook window) Application.Explorers.NewExplorer += OnNewExplorer; } private void OnNewExplorer(Outlook.Explorer newExplorer) { newExplorer.ViewSwitch += OnViewSwitch; UpdateRibbonButtons(newExplorer); } private void OnViewSwitch() { UpdateRibbonButtons(Application.ActiveExplorer()); } private void UpdateRibbonButtons(Outlook.Explorer explorer) { bool isTodayViewActive = IsTodayView(explorer); // Replace with your actual ribbon button reference if (Globals.Ribbons.MyCustomRibbon != null) { Globals.Ribbons.MyCustomRibbon.MyRestrictedButton.Enabled = !isTodayViewActive; } }
3. Ribbon Button Integration (Ribbon XML)
If you’re using Ribbon XML instead of the visual designer, hook into the getEnabled callback to dynamically toggle button state:
<button id="MyRestrictedButton" label="My Feature" getEnabled="GetButtonEnabled" />
Then add this method to your ribbon code-behind:
public bool GetButtonEnabled(Office.IRibbonControl control) { return !Globals.ThisAddIn.IsTodayView(Globals.ThisAddIn.Application.ActiveExplorer()); }
Key Notes
- Time Zones: The
CalendarView.StartandEndproperties use local time, so the date comparison works correctly across time zones. - Customized Views: If a user modifies the default Today View to show extra days (e.g., yesterday + today), this detection will fail—but this is an edge case for most users.
- Multiple Windows: The code handles multiple Outlook windows by subscribing to the
NewExplorerevent, ensuring all open windows are monitored.
内容的提问来源于stack exchange,提问作者Rick Harris

