I am just starting with Excel 2010 Add-in with VSTO and is quickly stumped by an exception generated by this code:
public void DoIt()
{
Excel.Range selectedRange = Application.Selection as Excel.Range;
if (selectedRange == null)
{
System.Windows.Forms.MessageBox.Show("Nothing selected");
}
else if(selectedRange.Cells.Count > 0)
{
selectedRange[1, 1].Value = "=2+3"; // exception on this line.
selectedRange[selectedRange.Rows.Count, selectedRange.Columns.Count].Value = "Birthday";
}
}
The exception can be reproduced by first clicking on a single cell in the worksheet, then click the formula bar and then run the above function (I call it via a Ribbon button).
Could anyone advise what is happening and how to handle this exception? Thanks you.