We came across an issue while writing a VSTO Outlook Add-in. Getting the subject of the composed email is working fine when doing ItemSend but when trying to get the subject while composing the email (before ItemSend) the retrieved subject is sometimes null. Its a preview function which is being triggered via a button on a ribbon.
Set a breakpoint and it looks like that ActiveInspector().CurrentItem is already not providing the right value as subject
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 should have the value from the mail subject but in the BAD CASE it is returning NULL.