错误 System.Runtime.InteropServices.COMException:代码使用 ExportAsFixedFormat 创建了一些 PDF,但随后出错

Error System.Runtime.InteropServices.COMException: Code creates some PDFs with ExportAsFixedFormat but then errors out

我在 Visual Studio 中的 VB.Net 程序创建 Excel 文件,然后将它们保存为 PDF,但是在这么多(30、40 或其他,不是重点)之后它出错了"Error System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'"。 可能我认为这是 Excel 得到 closed/released?
的方式 在我打开 Excel 文档并尝试将其另存为 PDF 后,错误来自这行代码。同样,这总是在一些文档已经保存为 PDF 后发生(有时 30,有时 50...):

xwb.ActiveSheet.ExportAsFixedFormat(0, "\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesPDF\" & originalCustomerName & " " & customerNumber & " " & todaysDate & ".pdf")

这是我的全部代码:

这将创建 EXCEL 文档并在其中填充数据:

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

最后将文档保存为 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()

非常感谢任何帮助。谢谢

********评论:** 我觉得 Excel 如何在两个 Excel 过程结束时得到 released/closed 有问题。我认为这是因为程序运行良好并将 Excel 文件保存为 PDF,但每次它都不会创建所有文件。在 "ExportAsFixedFormat" 所在的行创建了许多 PDF 文件后,它会停止。它永远不会停在同一个特定文件上,所以我会说任何特定的 PDF 都没有问题。

我主要将 "ExportAsFixedFormat" 更改为 "Printout",现在它似乎工作正常。感谢大家的帮助!

这是我现在拥有的代码:

Dim Path As String = "\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesExcel\" & originalCustomerName & " " & customerNumber & " 050720.xlsx"
                Dim Excel As Excel.Application = New Excel.Application
                Dim WorkBook As Excel.Workbook = Excel.Workbooks.Open(Path)
                Dim WorkSheets As Excel.Sheets = WorkBook.Sheets
                Dim WorkSheet As Excel.Worksheet = CType(WorkSheets(1), Microsoft.Office.Interop.Excel.Worksheet)
                Excel.DisplayAlerts = False
                Excel.Visible = False
                'SAVE AS PDF
                Dim totalFileName As String = "\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesPDF\" & originalCustomerName & " " & customerNumber & " " & todaysDate & ".pdf"
                Excel.ActiveSheet.Printout(Copies:=1, Preview:=False, ActivePrinter:="Microsoft Print to PDF", PrintToFile:=True, Collate:=True, PrToFileName:=totalFileName, IgnorePrintAreas:=False)