使用 .ActiveInspector().CurrentItem 时,检索到 "subject" 的组合电子邮件有时为空
Retrieved "subject" of composed email is sometimes null when using .ActiveInspector().CurrentItem
我们在编写 VSTO Outlook 加载项时遇到了问题。在执行 ItemSend 时,获取已撰写电子邮件的主题工作正常,但在撰写电子邮件时(在 ItemSend 之前)尝试获取主题时,检索到的主题有时为空。它是通过功能区上的按钮触发的预览功能。
设置一个断点,看起来 ActiveInspector().CurrentItem 已经没有提供正确的值作为主题
Ribbon_TabNewMailMessage.cs:
private void PreviewButton_Click(object sender, RibbonControlEventArgs e)
{
// pointing to ThisAddIn.cs (see code block below)
if (Globals.ThisAddIn.Application.ActiveInspector() != null)
{
// Obviously sometimes not catching subject
Outlook.MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
// BAD CASE: mailItem.Subject is sometimes NULL
var aSubj = mailItem.Subject;
ThisAddIn.cs:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
try
{
...
// Get the Application object
Outlook.Application application = this.Application;
// Subscribe to the ItemSend event, that it's triggered when an email is sent
application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(ItemSend_BeforeSend);
// new itemsend event
void ItemSend_BeforeSend(object item, ref bool cancel)
{
Outlook.MailItem mailItem = (Outlook.MailItem)item;
// GOOD CASE: this one is working properly !!!
var bSubj = mailItem.Subject;
mailItem.Subject 应该具有来自邮件主题的值,但在 BAD CASE 中它返回 NULL。
Subject
属性 在焦点离开主题编辑框之前不会更新。
您可以尝试找到编辑框 HWND 并使用 Windows API 检索其文本(GetWindowText 等)。
如果您需要获取最新更改,Save 方法可以提供帮助。它将 Microsoft Outlook 项目保存到当前文件夹,或者如果这是一个新项目,则保存到该项目类型的 Outlook 默认文件夹。
您也可以将光标切换到 window 中的另一个字段,以将更改传播到 Outlook 对象模型。 Outlook 缓存值,直到光标移动到另一个字段。这是处理 OOM 时的一个已知问题。
我们在编写 VSTO Outlook 加载项时遇到了问题。在执行 ItemSend 时,获取已撰写电子邮件的主题工作正常,但在撰写电子邮件时(在 ItemSend 之前)尝试获取主题时,检索到的主题有时为空。它是通过功能区上的按钮触发的预览功能。
设置一个断点,看起来 ActiveInspector().CurrentItem 已经没有提供正确的值作为主题
Ribbon_TabNewMailMessage.cs:
private void PreviewButton_Click(object sender, RibbonControlEventArgs e)
{
// pointing to ThisAddIn.cs (see code block below)
if (Globals.ThisAddIn.Application.ActiveInspector() != null)
{
// Obviously sometimes not catching subject
Outlook.MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
// BAD CASE: mailItem.Subject is sometimes NULL
var aSubj = mailItem.Subject;
ThisAddIn.cs:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
try
{
...
// Get the Application object
Outlook.Application application = this.Application;
// Subscribe to the ItemSend event, that it's triggered when an email is sent
application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(ItemSend_BeforeSend);
// new itemsend event
void ItemSend_BeforeSend(object item, ref bool cancel)
{
Outlook.MailItem mailItem = (Outlook.MailItem)item;
// GOOD CASE: this one is working properly !!!
var bSubj = mailItem.Subject;
mailItem.Subject 应该具有来自邮件主题的值,但在 BAD CASE 中它返回 NULL。
Subject
属性 在焦点离开主题编辑框之前不会更新。
您可以尝试找到编辑框 HWND 并使用 Windows API 检索其文本(GetWindowText 等)。
如果您需要获取最新更改,Save 方法可以提供帮助。它将 Microsoft Outlook 项目保存到当前文件夹,或者如果这是一个新项目,则保存到该项目类型的 Outlook 默认文件夹。
您也可以将光标切换到 window 中的另一个字段,以将更改传播到 Outlook 对象模型。 Outlook 缓存值,直到光标移动到另一个字段。这是处理 OOM 时的一个已知问题。