12

I'm opening an xlsx file and saving each sheet into a csv file.

Here is the relevant code for the saving:

int i=0;
foreach (Excel.Worksheet s in app.ActiveWorkbook.Sheets)
{
    s.Select(true); // Error here

    String outfile = outputpath + "("+i+")" + outputfilename + ".csv";
    wkb.SaveAs(outfile, Excel.XlFileFormat.xlCSVMSDOS);

    ++i;
}

There is no problem with the output file name or path, and the output file(s) do not exist. It saves the first two sheets then crashes. I tried with a different input file with 4 sheets and it worked perfectly, so it has to do with the input file.

The exception:

System.Runtime.InteropServices.COMException was unhandled
  HResult=-2146777998
  Message=Exception from HRESULT: 0x800AC472
  Source=ExcelXlsx2Csv
  ErrorCode=-2146777998
  StackTrace:
       at Microsoft.Office.Interop.Excel._Worksheet.Select(Object Replace)
       at ExcelXlsx2Csv.Program.Main(String[] args) in c:\Users\Edward\Documents\Visual Studio 2013\Projects\ExcelXlsx2Csv\ExcelXlsx2Csv\Program.cs:line 109
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Any hints appreciated!

3
  • 1
    what error are you getting specifically????
    – T McKeown
    Dec 23, 2013 at 5:27
  • LOL I just ran it and it saved them all. Ran it again, after deleting the files and it stopped at the 2nd sheet! The error is "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in... Additional information: Exception from HRESULT: 0x800AC472" Dec 23, 2013 at 5:30
  • Google "excel error 50290", lots of hits. Dec 23, 2013 at 9:00

3 Answers 3

24

In my case, the exception was been thrown because my excel Interop tools was displaying a modal dialog (associated with a expired licence key - shame on me). If I closed the dialog (which was been displayed in the background) and then hit 'continue' in Visual Studio, the program was able to connect to the xlsx file and retrive data succesfully.

1
  • Thanks bud this was our issue exactly. Sep 13, 2018 at 19:13
3

I think this is a binding issue, I just surrounded every call (SaveAs, Select) with a:

bool failed = false;
do
{
    try
    {
        // Call goes here
        failed = false;
    }
    catch (System.Runtime.InteropServices.COMException e)
    {
        failed = true;
    }
    System.Threading.Thread.Sleep(10);
} while (failed);
3
  • 26
    Turning an exception into a program hang. Nice. That problem solves itself when your customer uninstalls your program. Dec 23, 2013 at 9:04
  • 4
    That's the only way according to this post on MSDN forums. You can add a limit to the number of iterations or check that you only catch the right type of exception. Dec 23, 2013 at 20:23
  • 6
    I would say you "must" add a limit to the number of iterations, not "can"
    – PandaWood
    Jun 30, 2016 at 0:24
0

In my case there was a same exception and checked task manager, there was a lot EXCEL.exe opened, Kill them and then worked fine

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.