0
votes

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.

2

2 Answers

1
votes

The Save method can help if you need to get the latest changes. It saves the Microsoft Outlook item to the current folder or, if this is a new item, to the Outlook default folder for the item type.

Also you may switch the cursor to another field in the window to get changes propagated to the Outlook object model. Outlook caches values until the cursor is moved to another field. This is a known issue when dealing with OOM.

0
votes

Subject property will not get updated until the focus moves away from the Subject edit box.

You can try to find the edit box HWND and use Windows API to retrieve its text (GetWindowText etc.).