After reading your comments, I think that this problem (and others which might potentially come up) derives from a non-too-good communication with Excel. Thus, this question will consist just in showing you a structure which shouldn't provoke any problem.
Right at the start of the application (or when you start to analyse the given Excel file), you have to define the Excel Object, the WorkBook and the Worksheet you will be dealing with (the first one). I will focus on the Worksheet by following your example:
Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveSheet;
ws.SelectionChange += new Excel.DocEvents_SelectionChangeEventHandler(ColRangeSelChange);
Where ColRangeSelChange
is defined by:
public void ColRangeSelChange(Excel.Range target)
{
System.Windows.Forms.MessageBox.Show(target.AddressLocal);
}
While you deal with this spreadsheet you don't need to change this definition. Now the given method (ColRangeSelChange
) is associated with the given event (ColRangeSelChange) and will be called every time, the event is triggered. If you keep redefining the Worksheet and the Event, you might get in coordination-related problems and weird situations might occur.
If you want to account for a different spreadsheets (via ActiveSheet
again, or by any other mean), you would have to redo this process again (variable assignation and event assignation) with other variable or by keeping the same ones.
Summary: remove both Worksheet and Event definition from CreateColumn()
. Put this right before starting to interact with the given worksheet (before smartTemplateBtn_Click
). And make sure that you define events just once (at the start) and that you assign the given worksheet to a variable just once (at the start).