0

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");
                }
            }
        }
    }
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Josh
  • 101
  • 1
  • 4

2 Answers2

0

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 MailItemobjects.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • First, I am sorry for interrupting, `as you are expert on outlook`, could you help or advice on this question https://stackoverflow.com/questions/71881493/automatically-open-attached-workbook-when-i-open-specific-message-on-specific-ou – Waleed Apr 16 '22 at 11:33
  • Reminder exposed Class but Reminder.Item didn't seem to expose it. – Josh Apr 21 '22 at 22:32
0

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks for the refresher. For those that see this, under the Solution Explorer navigate to References and find the reference. In the properties, switch "Embed Interop Types" to false. It was set to True by default. I haven't been a "full time" dev in 10 years and I forgot about this but I remembered as soon as you mentioned it! – Josh Apr 21 '22 at 22:34
  • I fixed the embed statement but the check for "r.Item is Outlook.AppointmentItem" or any other valid Reminder item (appointment, meeting, task, and mailItem) always returns false. Any thoughts as to why that isn't working? I tried access the class property but it didn't seem to be available on Reminder.Item, just Reminder. – Josh Apr 21 '22 at 22:36
  • Got it! The Reminder.Item isn't accessible in the BeforeReminderShow event. I switched to ReminderFire and the statements worked just fine. I think I can work with that. Thank you both! – Josh Apr 21 '22 at 23:06