I am trying to set the value of cells in an Office 2010 worksheet using this code in Visual Studio 2012:
for (int i = 1; i < 5; i++)
{
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(i);
int columnCounter = 1;
foreach (string s in columnNames)
{
range = ((Excel.Range)xlWorkSheet.Cells[1, columnCounter]);
range.Value = s;
columnCounter++;
}
}
While stepping through this code it fails every time at the range.Value = s;
line and goes immediately to another method that closes Excel. It does not give any error messages and so I am lost as to why this isn't working. Can someone please help?