Reputation: 111
I'm trying to replace the default reminder dialog box. As part of that, I want to access more information about the item the reminder is placed on to include in the new dialog. I've seen old examples (Office 2013 or so) where probing Reminder.Item for type seems to work. However, in Outlook 2021 / VSTO in VS 2022, the if statements fall through even for items I know are MeetingItems.
When I stop and debug, I can get intellisense to open the dynamic view and I can tell it is a MeetingItem (has a subject, categories, organizer, etc.) but the type is just COM Object. Further, if I use the immediate window to cast as a MeetingItem I get this error: (r.Item as Outlook.MeetingItem).Categories error CS0433: The type 'MeetingItem' exists in both 'Microsoft.Office.Interop.Outlook, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' and 'Tasks, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
This is a fresh VSTO Outlook project where all I did was add the code below (I copied from my other project to ensure it wasn't an issue with the project). I didn't add any references, and the project isn't referencing itself. I assume that if you create a VSTO Outlook AddIn and then add this code, the same issues would arise.
Any thoughts as to what's going on here? Why can't I access Reminder.Item and why does VS think that MeetingItem exists in my project aside from the Office DLLs?
using Outlook = Microsoft.Office.Interop.Outlook; using Office = Microsoft.Office.Core;
namespace Tasks { public partial class ThisAddIn { private Outlook.Reminders m_Reminders;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
m_Reminders = Application.Reminders;
m_Reminders.BeforeReminderShow += Reminders_BeforeReminderShow;
}
private void Reminders_BeforeReminderShow(ref bool Cancel)
{
foreach (Outlook.Reminder r in m_Reminders)
{
if (r.IsVisible)
{
if (r.Item is Outlook.AppointmentItem)
{
Console.WriteLine("item is an appointment");
}
else if (r.Item is Outlook.MeetingItem)
{
Console.WriteLine("item is a meeting");
}
}
}
}
Upvotes: 1
Views: 221
Reputation: 49405
Your code is valid. But I have noticed the following error which is not related to casting Outlook objects at runtime:
(r.Item as Outlook.MeetingItem).Categories error CS0433: The type 'MeetingItem' exists in both 'Microsoft.Office.Interop.Outlook, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' and 'Tasks, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
It looks like interop types were embedded to the Tasks
assembly as well. You need to explicitly state what type are going to use (where it comes from) or just don't embed interop types into other assemblies to avoid having equivalent types in different assemblies.
Read more about the error in the Compiler Error CS0433 article.
Upvotes: 0
Reputation: 66255
Access the Class
property (all OOM objects expose it) from Reminder.Item
using late binding to figure out the item type. Note that you can also have TaskItem
and regular MailItem
objects.
Upvotes: 0