Monday, March 5, 2018

Date expiration (licensing) in a VSTO Office Add-In

Sometimes you want to have a VSTO Office add-in you've developed stop working on a specific date; this is usually done for licensing related purposes.

Here's how to gracefully stop a VSTO Office add-in from working before it even loads any Ribbon elements. Basically you have to modify the code in your ThisAddIn class to look like this:

    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        private Boolean isExpired()
        {
            DateTime end = DateTime.Parse("2018-03-21"); //This can be any date you like
            DateTime today = DateTime.Today;

            if (today >= end)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        protected override Microsoft.Office.Core.
            IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            if (!isExpired())
            {
                return new Ribbon1(); //Or whatever your ribbon class is called
            }
            else
                return null;
        }

        ... //Unrelated code from here on 
    }

Don't try to stop the Add-In from loading in the ThisAddIn_Startup() method as this gets called after the Add-In's Ribbon elements get loaded and display in the Office application.

No comments:

Post a Comment