I'm currently implementing a method that generated multiple sheets and export them as PDF. For this I'm using the Microsoft.Office.Interop Library (v14.0.0.0) with .NET 4.5.2 . Running Office is 2016
My code:
Dim excel As New Application()
excel.Visible = False
excel.DisplayAlerts = False
Dim workbooks As Workbooks
workbooks = excel.Workbooks
Dim workbook As Workbook = workbooks.Add(Type.Missing)
[...]
workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, String.Format(<a Path>)
ReleaseComObject(workSheet)
workbook.Close()
ReleaseComObject(workbook)
excel.Quit()
ReleaseComObject(excel)
The ReleaseComObject() looks like this (according to Microsoft Support):
Private Sub ReleaseComObject(objectToRelease As Object)
While System.Runtime.InteropServices.Marshal.ReleaseComObject(objectToRelease) > 0
End While
objectToRelease = Nothing
End Sub
This works fine if I run this code for one iteration BUT I noticed that the EXCEL-Process is still running.
If I try to do this in batch-mode (in the meaning of a for-loop) I get an excetion when entering the 2nd interation:
System.Runtime.InteropServices.COMException (0x800A03EC): Ausnahme von HRESULT: 0x800A03EC bei Microsoft.Office.Interop.Excel.WorkbookClass.ExportAsFixedFormat(XlFixedFormatType Type, Object Filename, Object Quality, Object IncludeDocProperties, Object IgnorePrintAreas, Object From, Object To, Object OpenAfterPublish, Object FixedFormatExtClassPtr) bei Controller.CreateListing(DataTable data, Int32 year, String mandantShortName) in ...
Line that throws exception:
workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, String.Format(<a Path>)
For reseach/testing I debugged before reentering the loop and killed the excel-process but w/o any changes.
Anyone faced this problem as well? Solutions/Suggestions?