Outlook功能区加载Inspector.CurrentItem为null [英] Outlook Ribbon Load Inspector.CurrentItem is null

查看:169
本文介绍了Outlook功能区加载Inspector.CurrentItem为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an Outlook add-in created with VSTO. The add-in has a single ribbon (visual designer) for the Mail.Compose ribbon type. The ribbon tab ControlIdType is set to "Custom". The only code in the add-in other than designer code is the following Load handler for the ribbon. this.Context.CurrentItem is unexpectedly returning 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为空.
  1. Open draft email. Ribbon loads OK.
  2. Open email from inbox.
  3. Open same draft email. Ribbon fails at step 2, inspector.CurrentItem is null.

注释

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

    • I've tested this in Outlook 2007, 2010 and 2013, with an Outlook 2007 and 2010 add-in created in VS2010, and Outlook 2010 add-in created in VS2012. All behave the same.
    • Repeatedly opening the draft email doesn't appear to cause the issue, an Email.Read inspector has to be opened in between.
    • The ribbon tab ControlidType matters. "Custom" will cause the issue, but the default option of "Office" doesn't exhibit the issue.
    • Flipping the scenario on its head and setting the ribbon type to Mail.Read gives the same result, provided the sequence of opening is reversed to Inbox > Draft > Inbox (fail).
    • All possible permutations of calls to Marshal.ReleaseComObject on the inspector and currentMailItem objects makes no difference.
    • 推荐答案

      迈克(Mike)的评论使我对行为更加好奇.在步骤1,RibbonComposeMail_Load事件称为一次.但是在第3步中,它称为两次.第一次在步骤3调用该事件,this.Context.CurrentItem为空,但是第二次调用该事件,则该属性保存电子邮件.

      Mikes comments helped reveal to me something a little more curious about the behaviour. At step 1, the RibbonComposeMail_Load event is called once. But at step 3 it is called twice. The first time the event is called at step 3, this.Context.CurrentItem is null, but the second time the event is called, the property holds the email.

      它正在将NewInspector事件中的项目值与功能区Load事件中的项目值进行比较,这使我注意到了这一点.因为步骤3中的事件顺序是:Ribbon_LoadNewInspectorRibbon_Load.我在ThisAddIn.CurrentMailItem中将邮件项的主题从Ribbon_Load转换为MessageBox.Show,但是很惊讶地看到它是打开的上一个电子邮件的主题,即步骤收件箱中的电子邮件2!

      It was comparing item values in the NewInspector event with those in the ribbon Load event which made me notice this. Because the sequence of events at step 3 is: Ribbon_Load, NewInspector, Ribbon_Load. I was getting Ribbon_Load to MessageBox.Show the subject of the mail item in ThisAddIn.CurrentMailItem, but was quite surprised to see it was the subject of the previous email opened, i.e. the inbox email at step 2!

      事实证明,解决方法是如果this.Context.CurrentItem为空,则忽略Ribbon_Load事件中的所有内容,因为第二个Ribbon_Load事件将使用正确的值设置触发.至于为什么,我们在示例的步骤3中看到了这种奇怪的行为,而在步骤1中却没有看到?对于实施Outlook对象模型的人员来说,这可能是个问题.

      As it turns out, the solution then is to ignore everything in the Ribbon_Load event if this.Context.CurrentItem is null, because a second Ribbon_Load event is about to be triggered with the correct values set. As to why we see this strange behaviour in step 3 of my example, but not step 1? That's probably a question for the people the implemented the Outlook Object Model.

      这篇关于Outlook功能区加载Inspector.CurrentItem为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆