I try to read a cell in an Excel worksheet using Excel Interop 15.0. At start my code look like this and it worked
Range myvar = (Worksheet.Cells[1,1]) as Range
For some unknown reasons, this code doesn't work anymore and I need to use
Range myvar = Worksheet.Cells[1,1]
Also, the code below thrown an InvalidCastException
Range myvar = (Range)(Worksheet.Cells[1,1])
Can someone tell me why the first code worked at start and now fails? And why the last example throws an exception ?
[Update - 2017-05-27 09:42]
Weird, here, my code:
if (!string.IsNullOrWhiteSpace((pBlueBookWorksheet.Cells[pBlueBookWorksheet.Range[aSetting.BlueBookCodeCellName].Row, i] as Range).Value.ToString()))
lCodeIndex.Add(Convert.ToString((pBlueBookWorksheet.Cells[pBlueBookWorksheet.Range[aSetting.BlueBookCodeCellName].Row, i] as Range).Value), i);
Yesterday and this morning before I try it didn't work. So I start removing 'as Range' but I noticed that some lines with 'as Range' work but some others don't. So, I decide to use 'as Range' again and MIRACLE, the program works ?!? It's as if writing the line again changed something in background. Oh also, lat night, I installed VS2017 Update 2
So If someone has an explanation to this phenomenon, send it
Thanks