Event Handlying



Using the COM Events

Add a Reference to the Excel Interop.
Assemblies, Extensions: "Microsoft.Office.Interop.Excel" (version 15.0.0.0)
Add a "using" reference to the top to provide a shorthand to this namespace.
Cast the "ExcelDnaUtil.Application" to the type "Microsoft.Office.Interop.Excel.Application" to get intellisense
Register an event handler for the AfterCalculate event.
The "AppEvents_AfterCalculateEventHandler" will not appear in the intellisense.

using Excel = Microsoft.Office.Interop.Excel; 

namespace ExcelDNALibrary
{
    public class Class1 : ExcelDna.Integration.IExcelAddIn
    {
        public void AutoOpen()
        {
            Excel.Application xlApp;
            xlApp = (Excel.Application)ExcelDna.Integration.ExcelDnaUtil.Application;

            xlApp.AfterCalculate += new Excel.AppEvents_AfterCalculateEventHandler(Method_RunAfterCalculate);
        }
        public void AutoClose()
        {
        }

        [ExcelDna.Integration.ExcelFunction(Category = "BETTER", Description = "HelloWorld", Name = "myFunction")]
        public static string myFunction([ExcelDna.Integration.ExcelArgument(Description = @"Text Value")] string sText)
        {
            object val = ExcelDna.Integration.ExcelAsyncUtil.Run(
                "myFunction",
                new object[] { sText },
                delegate
                {
                    return sText;
                });

            return (string)val;

        }
        public void Method_RunAfterCalculate()
        {
            System.Windows.Forms.MessageBox.Show("calculation has completed");
        }
    }
}



© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext