Search code examples
c#outlookoutlook-addinoffice-addinsribbonx

Enable/Disable Ribbon Control On Selection Change Of Mail Item


I am developing outlook add-in in C#.

In Project1, I am having class which adds controls like below :

 internal class ExplorerRibbonUi : MainRibbonUi
    {
        public ExplorerRibbonUi(Func<string, string> manifestLoader) :
            base(RibbonIds.Explorer, manifestLoader)
        {
            Controls.Add(new Controls1());
            Controls.Add(new Controls2());
        }
    }

I have added Control2 in Microsoft.Outlook.Explorer.xml file.

In Project2, I have a selection change event of mail item. Which is like below :

if (objSelectionList != null && objSelectionList.Count == 1 && objSelectionList[1] != null)
                {
                    Outlook.MailItem objMailItem = objSelectionList[1] as Outlook.MailItem;
                    if (objMailItem != null)
                    {
                        if (BrandingsManager.IsBrandingLoaded()
                            && (Api.Instance.LicenseManager.IsLicensed()
                            && (this.IsMailItemBodySecured(objMailItem) || this.IsMailItemSetForDecryption(objMailItem))
                            && this.IsRecipientSupportEncryption(objMailItem)))
                        {
                            //ENABLE RIBBON CONTROL
                        }
                        else
                        {
                            //DISABLE RIBBON CONTROL
                        }
                    }
                }

I want to enable/disable control of ribbon based on above condition. Note that, This is not VSTO add-in. I am not able to get ribbon controls in Project2 from Project1.


Solution

  • You can update the controls from the add-in where they were created only. And you can't access controls created by other add-ins until you have a public methods provided by the add-in developers. Consider using standard .net mechanisms such as Remoting for calling the code from another application. Also you may consider implementing a common interface in the add-in which can be consumed by others, see Call code in VSTO Add-ins from other Office solutions for more information.

    I'd recommend using ribbon callbacks instead. Note, you can customize the Ribbon UI by using callback procedures in COM add-ins. For each of the callbacks that the add-in implements, the responses are cached.

    For example, if an add-in writer implements the getEnabled callback procedure for a button, the function is called once, the state loads, and then if the state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.

    In the ribbon XML markup you need to specify the following callback to get an instance of the IRibbonUI interface which allows calling the Invalidate or InvalidateControl methods:

    <customUI … OnLoad="MyAddinInitialize" …>
    

    The callback accepts an instance of the IRibbonUI interface which should be cached for further usage when required to updated the state of controls.

    The getEnabled callback should have the following signature:

    C#: bool GetEnabled(IRibbonControl control)
    
    VBA: Sub GetEnabled(control As IRibbonControl, ByRef enabled)
    
    C++: HRESULT GetEnabled([in] IRibbonControl *pControl, [out, retval] VARIANT_BOOL *pvarfEnabled)
    
    Visual Basic: Function GetEnabled(control as IRibbonControl) As Boolean