My VB.Net program in Visual Studio creates Excel files and then saves them as PDF but after so many(30, 40 or whatever, not the point) it errors out saying "Error System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'".
Probably I am thinking it is the way Excel gets closed/released?
The error is from this line of code after I open an Excel document and try to save it as PDF. Again, that always happens after some documents have been already saved as PDF(sometimes 30, sometimes 50...):
xwb.ActiveSheet.ExportAsFixedFormat(0, "\\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesPDF\" & originalCustomerName & " " & customerNumber & " " & todaysDate & ".pdf")
Here is the whole code I have:
THIS CREATES THE EXCEL DOCUMENT AND FILLS IT WITH DATA:
Public Sub PopulateSheet(ByVal dt As Data.DataTable, ByVal File As String)
Dim oXL As Excel.Application = CType(CreateObject("Excel.Application"), Excel.Application)
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
oWB = oXL.Workbooks.Add
oSheet = CType(oWB.ActiveSheet, Excel.Worksheet)
'****Spreadsheet gets populated
......
'****Then
oWB.SaveAs(File)
oRng = Nothing
oXL.Quit()
GC.Collect()
GC.WaitForPendingFinalizers()
Marshal.FinalReleaseComObject(oXL)
Marshal.FinalReleaseComObject(oSheet)
Marshal.FinalReleaseComObject(oWB)
oSheet = Nothing
oWB = Nothing
oXL = Nothing
FINALLY THIS SAVES THE DOCUMENT AS PDF:
Dim xl As Object
Dim xwb As Object
xl = CreateObject("Excel.Application")
dt = CreateTable()
PopulateSheet(dt, "\\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesExcel\" & originalCustomerName & " " & customerNumber & " " & todaysDate & ".xlsx")
'****Open xlsx doc to save as pdf
xwb = xl.Workbooks.Open("\\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesExcel\" & originalCustomerName & " " & customerNumber & " " & todaysDate & ".xlsx")
xwb.ActiveSheet.PageSetup.Zoom = False
xwb.ActiveSheet.PageSetup.FitToPagesWide = 1
xwb.ActiveSheet.PageSetup.FitToPagesTall = False
'****Save as pdf
xwb.ActiveSheet.ExportAsFixedFormat(0, "\\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesPDF\" & originalCustomerName & " " & customerNumber & " " & todaysDate & ".pdf")
xl.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl)
xl = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(xwb)
xwb = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
Any help highly appreciated. Thank you
********COMMENT:** I feel there is a problem with how Excel gets released/closed at the end of the two Excel processes. I thought that because the program runs fine and saves the Excel files as PDF, but every time it never creates all the files. It stops after a number of PDF files get created at the line where there is "ExportAsFixedFormat". It never stops at the same specific file, so I would say there isn't a problem with any specific PDF.