0

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

  • 1
    Show the stack trace for that `TargetInvocationException`. – Eugene Podskal Jan 13 '17 at 19:42
  • Why not have a simple variable dim x as int x = 2018 and then in your code use X if it is > 0 or use DateTime.Now.Year otherwise? Also, you should compute the target file name once so you don't end up duplicating the filename string all over the place. – dgorti Jan 13 '17 at 19:58
  • @EugenePodskal I couldnt catch the exception, I tried with try{ ... } catch (TargetInvocationException TE) but it w as unable to catch it. – Sebastian Araneda Jan 13 '17 at 20:04
  • @dgorti I dont see how that would make it still work the next year or in two years time – Sebastian Araneda Jan 13 '17 at 20:04
  • @SebastianAraneda Then how do you know that it is thrown? Try http://stackoverflow.com/questions/8218249/how-do-i-enable-visual-studio-2010-to-break-when-a-first-chance-exception-happen - perhaps it happens somewhere else. – Eugene Podskal Jan 13 '17 at 20:12
  • @EugenePodskal Because the program stops and Visual Studio shows me the line with the exception, but I tried "try" and "catch (Exception e)" on the line to get the stacktrace but it doesnt seem to catch it because it crashes on the try part of the code – Sebastian Araneda Jan 13 '17 at 20:18
  • @SebastianAraneda Forgot about that one - http://stackoverflow.com/questions/2658908/why-is-targetinvocationexception-treated-as-uncaught-by-the-ide – Eugene Podskal Jan 13 '17 at 20:21
  • Of the solutions listed there I couldn't try neither because I already had disabled Just My Code and the second one is for an older version of Visual Studio (the one with the exceptions dialog) and I didn't know how to apply that on Visual Studio 2005 – Sebastian Araneda Jan 13 '17 at 20:30
  • Sorry forgot to tag you @EugenePodskal – Sebastian Araneda Jan 13 '17 at 20:52
  • Have you tried to just run the application outside the Visual Studio? Make top-level exception handler in `Main` and run the compiled exe from explorer. – Eugene Podskal Jan 13 '17 at 20:56

0 Answers0