0

I created a tool to convert Excel files. When the user convert a excel file the code create a Excel file first. When I'm on my system (Excel 2007) it's working without problems. When I install the program on a system with Excel 98 then it's throwing an exception. The first exception I got was another one, but also a HResult error. I fixed this through change the "SaveAs" to "SaveCopyAs". Then it was FIXED! Also for the other systems where Excel 98 is installed, but now I have another HResult error. What is the problem here:

            _savePath = sfd.FileName;

            MessageBox.Show("GOOD1");
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

            MessageBox.Show("GOOD2");
            // The exception is here on the workbook
            // HResult 8x00010105 (COMException)
            Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Add(Missing.Value);

            MessageBox.Show("GOOD3");
            workbook.SaveCopyAs(_savePath);

            MessageBox.Show("GOOD4");
            lblSavePath.Text = _savePath;
            workbook.Close(false, _savePath, Type.Missing);
            excelApp.Quit();

I hope someone could help me with this problem.

Thanks,

Jamie

Jamie
  • 363
  • 7
  • 19
  • What is the exception? Which line do you encounter the exception on? – Shakti Prakash Singh Jun 12 '13 at 08:09
  • http://i42.tinypic.com/xnhxq9.jpg – Jamie Jun 12 '13 at 08:12
  • It's on this line: Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Add(Missing.Value); – Jamie Jun 12 '13 at 08:15
  • There is no excel 98 version for excel. What is the installed version on the XP machine? – Shakti Prakash Singh Jun 12 '13 at 09:19
  • Also, can you specify what version of excel interop you are using?? – Shakti Prakash Singh Jun 12 '13 at 09:30
  • Sorry, it's Excel 97. And I'm using Excel 2007 on my system – Jamie Jun 12 '13 at 09:44
  • 1
    0x80010105 is RPC_E_SERVERFAULT, generated when the server (Excel in this case) crashed with an exception. That isn't exactly supposed to happen but you certainly increased the odds significantly by targeting such an ancient version of Excel. 1997 is a very long time ago in dog years. Even Windows 98 wasn't around yet back then. – Hans Passant Jun 12 '13 at 11:25
  • I am speechless after @HansPassant comment. I tried but am unable to find a system where Excel 97 is installed. I am not even sure if this is supposed to work with Excel 97. What version of Interop assembly are you using? Also, what is the need to target excel 97? Is it a specific requirement cause I don't see anyone using that. – Shakti Prakash Singh Jun 12 '13 at 11:45

2 Answers2

1

You might try:

_savePath = sfd.FileName;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
MessageBox.Show("GOOD2");
excelApp.SheetsInNewWorkbook = 1;      
try
{
   // Must be surrounded by try catch to work.
   excelApp.Visible = true;
}
catch (Exception e)
{
    Console.WriteLine("-------Error hiding the application-------");
    Console.WriteLine("Occured error might be: " + e.StackTrace);
} 
Microsoft.Office.Interop.Excel.Workbook workbook
workbook = excelApp.Workbooks.Open("your excel file", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // if working with another excel
excelApp.Workbooks.Add();
MessageBox.Show("GOOD3");
Excel.Worksheet sheetC = excelApp.Sheets.get_Item(1);   
sheetC.Name = "name-of-sheet";

workbook.SaveCopyAs(_savePath); // SaveAs should work actually.
workbook.Close();
excelApp.Quit(); 

I took your solution and modified the Missing.Value part which wasn't correct. Plus you don't really need to give parameters to the workbook.Close. Solution found here: I want to add only one sheet after creating an Excel workbook through C#

Community
  • 1
  • 1
mike27015
  • 686
  • 1
  • 6
  • 19
  • 1
    You tried to remove **excelApp.SheetsInNewWorkbook = 1;** and check if you don't give some **null** values as input somewhere? Also the **Type.Missing**, did you include the correct class **System.Type**? Did you include the Microsoft.Office.Interop libraries to your project? – mike27015 Jun 12 '13 at 09:45
0

Maybe try your code without the excelApp.Quit(); line.

Use the excelApp.Quit(); function only if you are not going to use the excelApp object again.

Pabinator
  • 1,601
  • 1
  • 21
  • 25