Question

I have a winform dialog in my excel add-in, which is popped up on click of a panel button. I have a selection change event added to worksheet. The event is not being fired first time. I have to close the dialog and the open it again, This time it will work. Am I missing something here, or it is bug with excel interop API?

Enviornment:Excel 2007, .NET 4.0, Interop runtime: v1.1.4322

Following is the code

public partial class CreateColumn : Form

    {

    public CreateColumn()
    {
        InitializeComponent();
        Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveSheet;
        //Bug: this event does not fire the first time.. works on second time.
        ws.SelectionChange += new   Excel.DocEvents_SelectionChangeEventHandler(ColRangeSelChange);

    }

    public void ColRangeSelChange(Excel.Range target) 
    {
        System.Windows.Forms.MessageBox.Show(target.AddressLocal);
    }}

This is how Create Column is being called

 private void smartTemplateBtn_Click(object sender, EventArgs e)
 {
      Range SelectedRange = Globals.ThisAddIn.Application.Selection;
      if (SelectedRange != null)
      {
           List<string> DataSetLabels = new List<string>();
           foreach (Range cell in SelectedRange.Cells)
           {
                if (cell.Value2 != null && !cell.Value2.Equals(""))
                {
                    if (!DataSetLabels.Contains(cell.Value2))
                    {
                        DataSetLabels.Add(cell.Value2);
                    }
                }
           }
           if (DataSetLabels.Count > 0)
           {
               PopupCreateColumnDialog(DataSetLabels);
           }
      }
 }

 public void PopupCreateColumnDialog(List<string> DataSetLabels)
 {
      if (DataSetLabels.Count > 0)
      {
           CreateColumn colDialog = new CreateColumn();
           colDialog.TopMost = true;
           colDialog.Show();
      }
 }
Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top