0

I have a VSTO Outlook 2016 Add-in which has only a checkbox on it.

Scenario: While replying to an email, the checkbox has to be checked. Upon sending the email, I'm adding a 'mailItem.FlagRequest' text to the email saying 'This email is a resolution response for the case'. When the sent email is opened from Sent Items of Outlook, the checkbox has to be remained checked.

Problem: Upon sending the email with checkbox checked, if I open the email from Sent Items - I can see the checkbox being checked for the first time alone. If I close the email and open again, the checkbox is unchecked.

I tried to explicitly check the checkbox, but it wont get checked.

Code link here:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;

namespace OutlookAddIn5
{
    public partial class ThisAddIn
    {
        private Outlook.Inspectors _appInspectors;
        ThisRibbonCollection ribbonCollection;
        private Outlook.Explorer explorer;
        private Outlook.Application app;
        private Outlook.MailItem mailItem;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            explorer = this.Application.ActiveExplorer();
            app = (Outlook.Application)explorer.Application;

            _appInspectors = app.Inspectors;
            _appInspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

            this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
        }

        void Application_ItemSend(object Item, ref bool Cancel)
        {
            ThisRibbonCollection ribbonCollection;
            ribbonCollection = Globals.Ribbons[Globals.ThisAddIn.Application.ActiveInspector()];
            if (ribbonCollection.Ribbon1.checkBox1.Checked)
            {
                Outlook.MailItem mi = Item as Outlook.MailItem;
                mi.FlagRequest = "This email is a resolution response for the case";
                mi.Save();
            }
        }

        void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {
            mailItem = Inspector.CurrentItem as Outlook.MailItem;
            ribbonCollection = Globals.Ribbons[Globals.ThisAddIn.Application.ActiveInspector()];
            if (!string.IsNullOrEmpty(mailItem.FlagRequest) && mailItem.FlagRequest.Contains("This email is a resolution response for the case"))
            {
                ribbonCollection.Ribbon1.checkBox1.Checked = true;
                ribbonCollection.Ribbon1.checkBox1.Enabled = false;
                ribbonCollection.Ribbon1.checkBox1.Visible = true;               
            }
            else
            {
                //   Globals.Ribbons.Ribbon1.checkBox1.Visible = false;
                ribbonCollection.Ribbon1.checkBox1.Checked = false;
                ribbonCollection.Ribbon1.checkBox1.Enabled = true;
                ribbonCollection.Ribbon1.checkBox1.Visible = false;              
            }           
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Note: Outlook no longer raises this event. If you have code that 
            //    must run when Outlook shuts down, see http://go.microsoft.com/fwlink/?LinkId=506785
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }        
        #endregion
    }
}
  • @JeremyThompson added the code as requested. Thanks. – Ramzz Sep 20 at 10:13
  • So is the Inspectors_NewInspector event firing when you open the email again? Or is that event working but the mailItem.FlagRequest is null or empty? – Jeremy Thompson Sep 20 at 11:07
  • @jeremy - the inspector event is firing, flagrequest also has data, thus the if condition gets true and checkbox is being set to checked programmatically. However, on the email form, the checkbox on the ribbon is still showing unchecked :( – Ramzz Sep 20 at 15:57
0

You need to provide an event handler for your check box control. When the event fires, set a custom property on the MailItem (or the FlagRequest property)

When a item is opened (Inspectors.NewInspector event), explicitly refresh the ribbon. When Outlook invokes a callback for the checkbox checked state, return the value based on the custom property (or FlagRequest in your case).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.