Reputation: 373
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
Upvotes: 0
Views: 3844
Reputation: 1641
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.
Upvotes: 0
Reputation: 676
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#
Upvotes: 1