gpt4 book ai didi

c# - Outlook 功能区加载检查器。CurrentItem 为 null

转载 作者:行者123 更新时间:2023-12-02 19:36:04 31 4
gpt4 key购买 nike

概述

我有一个使用 VSTO 创建的 Outlook 加载项。该加载项有一个用于 Mail.Compose 功能区类型的功能区(可视化设计器)。功能区选项卡 ControlIdType 设置为“自定义”。加载项中除设计器代码之外的唯一代码是功能区的以下 Load 处理程序。 this.Context.CurrentItem 意外返回 null。

代码

private void RibbonComposeMail_Load(object sender, RibbonUIEventArgs e)
{
try
{
var inspector = this.Context as Outlook.Inspector;
if (inspector == null)
{
throw new ApplicationException("Fail - Step 1");
}

var currentMailItem = inspector.CurrentItem as Outlook.MailItem;
if (currentMailItem == null)
{
throw new ApplicationException("Fail - Step 2");
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

步骤

  1. 打开电子邮件草稿。功能区加载正常。
  2. 从收件箱打开电子邮件。
  3. 打开同一封电子邮件草稿。功能区在第 2 步失败,inspector.CurrentItem 为 null。

注释

  • 我已在 Outlook 2007、2010 和 2013 中对此进行了测试,其中使用了在 VS2010 中创建的 Outlook 2007 和 2010 加载项以及在 VS2012 中创建的 Outlook 2010 加载项。所有人的行为都是一样的。
  • 重复打开电子邮件草稿似乎不会导致问题,必须在其间打开 Email.Read 检查器。
  • 功能区选项卡 ControlidType 很重要。 “自定义”会导致该问题,但默认选项“Office”不会出现该问题。
  • 将场景翻转过来并将功能区类型设置为 Mail.Read 会得到相同的结果,前提是打开顺序相反为“收件箱”>“草稿”>“收件箱”(失败)。
  • inspectorcurrentMailItem 对象上调用 Marshal.ReleaseComObject 的所有可能排列都没有区别。

最佳答案

我自己也遇到了同样的问题。

我为 Outlook 日历约会设计了一个功能区栏,其中包含一些我想在每个约会中保存的额外字段(例如“此 session 是否可以节省差旅费?”)

我成功了,但是,做起来很棘手。

正如您所说,当您的 Ribbon1_Load 函数启动时,ActiveInspector() 为 null...那么您应该如何获取有关当前状态的详细信息电子邮件消息或日历约会?

这是您需要执行的操作。此示例基于日历约会,但很容易适应电子邮件项目。

首先,在 ThisAddIn.cs 文件中,您需要进行一些更改:

public partial class ThisAddIn
{
Outlook.Inspectors inspectors;
public static Outlook.AppointmentItem theCurrentAppointment;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
inspectors = this.Application.Inspectors;
inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
}

当用户打开或创建新的 Outlook 项目时,我们的“Inspectors_NewInspector”函数将被调用,此时,我们能够获取有关该项目的详细信息项目:

void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
// This function (apparently) gets kicked off whenever a user opens a new or existing item
// in Outlook (Calendar appointment, Email, etc).
// We can intercept it, modify it's properties, before letting our Ribbon know about it's existance.
//
theCurrentAppointment = null;

object item = Inspector.CurrentItem;
if (item == null)
return;

if (!(item is Outlook.AppointmentItem))
return;

theCurrentAppointment = Inspector.CurrentItem as Outlook.AppointmentItem;
}

使用此代码后,我们可以调整 Ribbon1_Load 函数来获取此“theCurrentAppointment”变量,并详细阅读有关用户正在创建/修改的日历约会的详细信息。

private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
// When this function gets called, "Globals.ThisAddIn.Application.ActiveInspector()" is always NULL, so we have
// to fetch the selected AppointmentItem via the ThisAddIn class.

if (ThisAddIn.theCurrentAppointment != null)
{
// Our Ribbon control contains a TextBox called "tbSubject"
tbSubject.Text = ThisAddIn.theCurrentAppointment.Subject
}
}

关于c# - Outlook 功能区加载检查器。CurrentItem 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18458338/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com