I am making a program to manage different projects for a company, the idea is that you put some info and it writes it into an excel file.
The excel files are names ("FINANZAS - ") + Current Year
The idea is that it detects the current year with DateTime and if the excel file for that year doesnt exists it creates it. This is my code:
MyApp = new Excel.Application();
MyApp.Visible = false;
if (!File.Exists("FINANZAS - " + DateTime.Now.Year.ToString() + ".xlsx")) { File.Copy(rutaPlantilla, "FINANZAS ARAINCO - " + DateTime.Now.Year.ToString() + ".xlsx"); }
MyBook = MyApp.Workbooks.Open(@"C:\Users\Sebastian\Documents\Visual Studio 2015\Projects\Finanzas ARAINCO\Finanzas ARAINCO\bin\Debug\FINANZAS ARAINCO - " + DateTime.Now.Year.ToString() + ".xlsx");
MySheet = (Excel.Worksheet)MyBook.Sheets[1];
lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row + 1;
The problem comes when I try changing the date to test it with other years, I tried switching it to 2018 for testing but it throws me a TargetInvocationException on the last row (the one that, ironically, I use to detect the last row on excel)
I dont know if this is caused by something on the excel part of the code or if it has something to do with changing the date manually to 2018