Event Handlers and UDFs in the automation add-ins

Event Handlers and UDFs in the automation add-ins

Post by cGFybWFuYW » Fri, 09 Feb 2007 23:16:01

Great ..thanks to this site for helping.
I have an issue and have followed the following approach to create
automation add-in.
and created following code

using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Text;
using Microsoft.Office.Interop.Excel;

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Diagnostics;

namespace lnzv2
public class wsf
Excel.DocEvents_ChangeEventHandler EventDel_CellsChange;

Excel.Application xlApp;
Excel.Workbook xlBook;
Excel.Worksheet xlSheet1, xlSheet2, xlSheet3;

public wsf()



public void myInitialization()
xlApp = new Excel.Application();
xlBook = xlApp.Workbooks.Add(Missing.Value);
xlBook.Windows.get_Item(1).Caption = "XL Event Test";
xlSheet1 = (Excel.Worksheet)xlBook.Worksheets.get_Item(1);
xlSheet2 = (Excel.Worksheet)xlBook.Worksheets.get_Item(2);
xlSheet3 = (Excel.Worksheet)xlBook.Worksheets.get_Item(3);
EventDel_CellsChange = new Excel.DocEvents_ChangeEventHandler(
xlSheet1.Change += EventDel_CellsChange; // ........question1


private void CellsChange(Excel.Range Target )


public int NumberOfCells(object Range)

int [,] TheValues = new int[1, 1];
TheValues[0, 0] = 1;
TheValues[0, 1] = 2;
TheValues[1, 0] = 3;
TheValues[1, 1] = 4;

Excel.Range r = Range as Excel.Range;


return r.Cells.Count;

Given question1 I am unable to invoke the event.So please give me an idea
how the event can be used and correct me if i am wrong.
In question2 I am unable to get the value in the cells of the given range
passes in the function and it gives an error in the cell where i have written
the UDF =NumberOfCells(b2:c3).

Please give a heling hand.