在我的新工作中开始使用Outlook 2007 Addin进行开发后,用户在Outlook启动期间遇到以下错误: Object reference not set to an instance of an object
. 尝试通过额外的尝试捕获来跟踪问题,因为来自未抑制的警报的初始VSTO异常消息不是特别有用 . 我跟踪了以此代码开头的方法的问题:
if (newToolBar == null)
{
Office.CommandBars cmdBars = this.Application.ActiveExplorer().CommandBars;
newToolBar = cmdBars.Add("Data Team Toolbar", Office.MsoBarPosition.msoBarTop, false, true);
}
作为VSTO的新手,我的第一个假设是ActiveExplorer() . CommandBars返回null . 在与我的最终用户进一步通信之后,我能够确认他们已经使用Excel的邮件菜单中的发送邮件选项启动了Outlook . (这不是我预见过的场景 . )这似乎与我的主要Outlook窗口的理论不合作,并且我能够成功地重现崩溃 .
因此我添加了以下代码
if (this.Application.ActiveExplorer() == null )
{
// If Outlook is opened via some other method ie. send on Excel or Word
// Kill Addin nicely.
this.OnShutdown();
}
但是,我很快意识到我在UAT下发布这个版本(因为我似乎无法找到在Visual Studio中重现问题的方法(命令开关(https://support.office.com/en-sg/article/Command-line-switches-for-Microsoft-Office-Outlook-2007-92de9e0b-4f97-42a2-8e02-89c4a8294916))具体"/c ipm.note"设置为调试参数似乎对调试启动过程没有影响 .
根据这个链接(VSTO Outlook integration - Outlook shutdown event for synchronization),这似乎关闭Outlook,而不仅仅是我想到的Addin .
我从设计预期(虽然不理想)中想到的唯一解决方案是禁止Addin在这种情况下采取进一步行动,但允许它在正常启动条件下再次运行而无需用户干预 . 我认为逻辑可以包含在这样的保护条款中,
if (this.Application.ActiveExplorer().Caption != "Microsoft Outlook")
{
//Disabling code//
}
但是我不确定我是如何或者是否要实现这个,因为我的Addin在工具栏上有很多依赖(这个解决方案意味着需要添加许多非常具体的保护条款来检查存在的工具栏是否需要添加我宁愿避免,如果可能的话 . 理想情况下,我希望Addin能够检测它是否以不同的方式运行,如果以这种方式打开,只有当Outlook的主窗口可见时才加载工具栏,我已经有了一个背景主循环作为其他要求的一部分 . 所以可能是某些东西?
if(DataTeamAddin.LoadStyle != "Normal")
{
if(OutlookMainWindowVisible == true && ToolbarIsNotLoaded == true)
{
BeginToolbarSetup();
}
else
{
//Resume Main Loop
}
}
但是我不确定该怎么办呢?更不用说如何使用MSTest中的自动化测试对此进行测试,因为我甚至无法在VS2013 IDE中重现该问题?
2 回答
首先,不推荐使用命令栏 . 请考虑使用Fluent UI . 您可以在MSDN的以下系列文章中阅读有关新UI的更多信息:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
其次,从其他Office应用程序运行Outlook时,这是一个已知的案例 . 只有检查员窗口可用 . 您可以订阅NewInspector或InspectorActivate事件,以了解在使用“发送到”命令时运行加载项 .
一切就绪后你能运行你的代码吗?
https://msdn.microsoft.com/en-us/library/bb147656(v=office.12).aspx