Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
We have a code that exports some data to excel, then uses that excel file to generate a pdf file using the exportAsFixedFormat function. Now it is failing with system exception regularly.

the below is the code
oBook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,
                          paramExportFilePath, paramExportQuality,
                          paramIncludeDocProps, paramIgnorePrintAreas,
                          paramFromPage, paramToPage, paramOpenAfterPublish)

            ' Close book
            oBook.Close()
            '
            'Exit Excel
            oExcel.Quit()


below is the exception error we are getting
System.Exception: System.Exception: System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC     at Microsoft.Office.Interop.Excel._Workbook.ExportAsFixedFormat(XlFixedFormatType Type, Object Filename, Object Quality, Object IncludeDocProperties, Object IgnorePrintAreas, Object From, Object To, Object OpenAfterPublish, Object FixedFormatExtClassPtr)

What I have tried:

Please provide suggestions for a permanent fix
Posted
Updated 28-Feb-22 4:32am
Comments
Richard MacCutchan 28-Feb-22 4:34am    
The error code appears to be a catch all, you may need to try Microsoft's help forums.

Just a wild guess, as you didn't show much of the code (nor the line that throws):

maybe your code applied some changes to the workbook, and Excel wants to know whether it should save or not.

If you want to close without saving, just set
oBook.Saved = true
before closing.

:)
 
Share this answer
 
Take a look here: How to properly release Excel COM objects: C# code examples[^]

Quote:
C#
Excel.Application app = null;
Excel.Workbooks books = null;
Excel.Workbook book = null;
Excel.Sheets sheets = null;
Excel.Worksheet sheet = null;
Excel.Range range = null;
 
try
{
    app = new Excel.Application();
    books = app.Workbooks;
    book = books.Add();
    sheets = book.Sheets;
    sheet = sheets.Add();
    range = sheet.Range["A1"];
    range.Value = "Lorem Ipsum";
    book.SaveAs(@"C:\Temp\ExcelBook" + DateTime.Now.Millisecond + ".xlsx");
    book.Close();
    app.Quit();
}
finally
{
    if (range != null) Marshal.ReleaseComObject(range);
    if (sheet != null) Marshal.ReleaseComObject(sheet);
    if (sheets != null) Marshal.ReleaseComObject(sheets);
    if (book != null) Marshal.ReleaseComObject(book);
    if (books != null) Marshal.ReleaseComObject(books);
    if (app != null) Marshal.ReleaseComObject(app);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900