Search code examples
c#outlookoutlook-addinselectionoffice-addins

How To Make 'Reply' & 'Reply-All' Button Grey In Outlook C#


In outlook…

  • If you select multiple email from top to bottom, Reply/Reply-all buttons are enabled
  • If you select multiple email from bottom to top, Reply/Reply-all buttons are disabled

CASE-1 : Selecting 'Test 2' & then select 'Test 1' => Reply/Reply-all buttons are enabled

enter image description here

CASE-2 : Selecting 'Test 1' & then select 'Test 2' => Reply/Reply-all buttons are disabled

enter image description here

I have Explorer.SelectionChange event which checks if there are multiple emails are selected. If there are multiple emails are selected, Then I have disabled 'Reply/Reply-All' events by MailItem.Actions["Reply"].Enabled = false;.

Explorer objExplorer = (Explorer)Api.Instance.GetActiveExplorer();
if (objExplorer != null)
{
    objExplorer.SelectionChange += () =>
    {
        Selection objSelectionList = objExplorer.Selection;
        if (objSelectionList != null && objSelectionList.Count > 1 && objSelectionList[1] != null)
        {
            MailItem objMailItem = objSelectionList[1] as MailItem;
            if (objMailItem != null)
            {
                objMailItem.Actions["Reply"].Enabled = false;
                objMailItem.Actions["Reply to All"].Enabled = false;
            }
        }
    };
}

But it's not giving me output like CASE-2. So basically I want to make both buttons disabled in CASE-1 as shown in CASE-2. It's only giving me a popup saying 'This action not available for this item'.

I have to do this to maintain consistency in both case. So need solution from one of below..

  1. If there is any way to enable both buttons in CASE-2 (Like CASE-1)
  2. If there is any way to disable both buttons in CASE-1 (Like CASE-2)

Thanks in advance.


Solution

  • In the code you disable the Reply and Reply to All actions on the first selected item which is a different item in both scenarios:

    MailItem objMailItem = objSelectionList[1] as MailItem;
    if (objMailItem != null)
    {
       objMailItem.Actions["Reply"].Enabled = false;
       objMailItem.Actions["Reply to All"].Enabled = false;
    }
    

    But the displayed item is always the same, but it is not necessary the first in the collection. So, instead I'd suggest iterating over all selected items and disabling actions for all of them to make sure the view is updated correctly. A raw sketch:

    for(int i=1; i <= objSelectionList.Count; i++)
    {
      MailItem objMailItem = objSelectionList[i] as MailItem;
      if (objMailItem != null)
      {
        objMailItem.Actions["Reply"].Enabled = false;
        objMailItem.Actions["Reply to All"].Enabled = false;
      }
    }