It looks like the exception is thrown from the oSheet.Cells[1, 1]
and oSheet.Cells[4, 4]
used as arguments to get_range
.
Applying the following, no exception will be thrown:
Excel.Range c1 = oSheet.Cells[1, 1];
Excel.Range c2 = oSheet.Cells[4, 4];
oRange = (Excel.Range)oSheet.get_Range(c1, c2);
oRange.EntireColumn.AutoFit();
So, it might be related to the oSheet.get_Range
functionality. It receives an object as an argument, therefore it might try to invoke a get_Range
method on the arguments to receive the internal cell, and the up-cast from Range
to object done by the compiler might hide the method call.
If you need the cells definition by row/column – try using the above approach.