0

I'm opening an Excel file in .NET 4.0 (C#). I'm using the 12.0 version of interop. The Excel file is an xls (old). I can open it just fine, but workbook.close() causes a COM Exception with no real details around it.

oExcel = new Application();
oExcel.Visible = true;
oExcel.DisplayAlerts = false;

oBooks = oExcel.Workbooks;
oBook = null;
oBook = oBooks.Open(finalFilename);

oBook.Close();   // this gives COM Exception

Any ideas what I'm doing wrong?

Here is the interesting thing. I don't have to save this. After it does what it needs to do, I pull the data I need out and store it and the workbook I just made a copy of can just go away.

user441521
  • 6,942
  • 23
  • 88
  • 160

3 Answers3

0

In my experience, errors on just opening and closing most likely indicates an issue with the file. Excel desktop is pretty forgiving and will gracefully handle any problem with the file. The file may look and work fine in Excel without giving any indication that something is wrong.

You could try re-saving the file in Excel. If it still doesn't work, try creating a fresh spreadsheet and copying the contents from the old file.

ytran
  • 120
  • 6
  • So I saved a blank workbook out there and it works. However this isn't an option as I have to work with these generated workbooks that have a ton of vba/dll code in it. This workbook has an addin which is digitally signed so not sure if that is causing issues maybe? I don't care about the signature but how can I find out what would be causing this close issue? – user441521 Sep 06 '13 at 17:14
  • I'm afraid I don't have a better answer. If the workbook was pre-generated by a 3rd-party software then any of a number of things could be wrong. My gut feeling is that you'd have to re-create the workbook and save a lot of headaches, even if it takes a bit of copying-and-pasting. – ytran Sep 06 '13 at 18:27
  • Thanks ytran. This is basically an application they made and not your normal workbook so that's out of the question. I'm getting closer though. I injected a function into the VBA project from C# that closes the workbook from VBA. This works, but throws an error when it comes back to C# but I just trap it and ignore it. Everything is working now except that EXCEL.exe process stays running. Just need to figure out what I'm doing wrong with that. – user441521 Sep 06 '13 at 18:30
0

Workbook.Close does not appear to contain a method Close without parameters.

http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.close.aspx

Method Signature: void Close( Object SaveChanges, Object Filename, Object RouteWorkbook )

        object missing = Type.Missing;
        object noSave = false;
        oBook.Close(noSave, missing, missing);
tim
  • 526
  • 2
  • 9
0
        Excel.Application objExcel = new Excel.Application();
        Excel.Workbook objWorkbook = (Excel.Workbook)(objExcel.Workbooks._Open(@"D:\a.xls", true,
        false, Missing.Value, Missing.Value, Missing.Value,
        Missing.Value, Missing.Value, Missing.Value,
        Missing.Value, Missing.Value, Missing.Value,
        Missing.Value));

your code...

            objWorkbook.Save();         
            objWorkbook.Close(true, @"C:\Documents and Settings\Administrator\b.xls", true);
            objExcel.Quit();
Ehsan Keramat
  • 164
  • 2
  • 6
  • Try casting the parameters as objects – tim Sep 06 '13 at 17:14
  • So when I call Save() it prompts me about the add-in xla project "A file named 'addin name here' alrady exists in this location. Do you want to replace it. Which doesn't really make sense to me as to why it thinks it's in the same location I opened the file in because I don't see it there. It's almost as if it's trying to save it in where the xla is installed. Not sure why it would try to do that. I would think the Save() would only look at the workbook itself and not the xla. If I save from desktop excel it never prompts me with this. – user441521 Sep 06 '13 at 17:21
  • Excel does not quit because your app is still holding references to COM objects.I guess you're invoking at least one member of a COM object without assigning it to a variable.Never use 2 dots with com objects... – Ehsan Keramat Sep 06 '13 at 17:24
  • I'm not at this time. If I leave out the Save() and Close() I get a prompt asking me to save or not. If I manually select save, then everything ends correctly and the EXCEL process finishes. So with having the Save() function present I get this xla prompt saying to overwrite the original xla file, which if I could remove that prompt I think this would work. – user441521 Sep 06 '13 at 17:28
  • It's not usual in programming; remove and save, look at this, maybe help : http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects – Ehsan Keramat Sep 06 '13 at 17:45