我多年来一直将这个代码用于我的自定义标签(Ribbon Designer,TabReadMessage,用于当前打开的 e-mail)。但是,在今年夏天 Office 更新之后,它使我在代码的第一行中出错。

Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
    Dim mailItem As Outlook.MailItem = TryCast(Globals.ThisAddIn.Application.ActiveInspector.CurrentItem, Outlook.MailItem)
    ' do stuff
End Sub
  • 在 Outlook 2013 上运行此插件不会产生任何错误。

  • 在 Outlook 365 内部版本 1705 上运行-没问题。

  • 在 Outlook 365 内部版本 1706 及更高版本上运行-给我错误。

第一次打开 e-mail 时,它工作正常,但是关闭 e-mail 并再次打开时,出现错误(调试时):“ NullReferenceException occured

有人可以向我解释为什么吗?也许您可以重现该错误?

我有 Visual Studio 2012(VSTO VB.Net)。这是我要做的复制:

  • 创建一个 Outlook 2013 add-in

  • 添加一个 Ribbon 项目(可视设计器),并将 RibbonType 设置为“ Microsoft.Outlook.Mail.Read”。

  • 将选项卡更改为 TabReadMessage。

  • 将一个按钮添加到选项卡。

  • 设置“调试”,“异常”,“公共语言运行时异常”,“系统”,然后在 System.NullReferenceException 中选中“引发”。

  • 将代码添加到 Ribbon1_Load 和 run/debugg:

Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
    Dim mailItem As Outlook.MailItem = TryCast(Globals.ThisAddIn.Application.ActiveInspector.CurrentItem, Outlook.MailItem)
    MsgBox(mailItem.Subject)
End Sub