我正在尝试替换默认的提醒对话框。作为其中的一部分,我想访问更多关于提醒放置在新对话框中的条目的信息。我见过一些旧的例子(Office 2013左右),在这些例子中,探测Reminder.Item的类型似乎很有效。但是,在VS 2022中的Outlook2021/ VSTO中,即使我知道的项是MeetingItems,if语句也会失效。
当我停止和调试时,我可以让intellisense打开动态视图,并且我可以告诉它是一个MeetingItem (有一个主题、类别、组织者等)。但类型只是COM对象。此外,如果使用即时窗口强制转换为MeetingItem,则会得到以下错误:(r.Item as Outlook.MeetingItem).Categories error CS0433:类型'MeetingItem‘同时存在于'Microsoft.Office.Interop.Outlook、Version=15.0.0.0、Culture=neutral、PublicKeyToken=71e9bce111e9429c’和‘任务、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null’中。
这是一个新的VSTO Outlook项目,我所做的就是添加下面的代码(我从我的另一个项目中复制,以确保它不是项目的问题)。我没有添加任何引用,而且项目本身也没有引用。我假设,如果您创建一个VSTO Outlook AddIn,然后添加此代码,则会出现相同的问题。
对这里发生了什么有什么想法吗?为什么我不能访问Reminder.Item,为什么VS认为除了Office之外,MeetingItem还存在于我的项目中?
使用Outlook = Microsoft.Office.Interop.Outlook;使用Office =Microsoft.Office.Core
命名空间任务{公共部分类ThisAddIn {私有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");
}
}
}
}发布于 2022-04-15 23:26:03
使用后期绑定从Reminder.Item访问Reminder.Item属性(所有OOM对象都公开它),以确定项类型。请注意,您还可以拥有TaskItem和常规MailItem对象。
发布于 2022-04-16 19:47:52
你的代码是有效的。但是,我注意到以下错误与在运行时转换Outlook对象无关:
(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'看起来互操作类型也嵌入到Tasks程序集中。您需要显式地声明将使用什么类型(它来自何处),或者只是不要将互操作类型嵌入到其他程序集中,以避免在不同的程序集中具有等效的类型。
阅读编译器错误CS0433文章中有关错误的更多信息。
https://stackoverflow.com/questions/71889086
复制相似问题