首页 > 解决方案 > 错误 System.Runtime.InteropServices.COMException:代码使用 ExportAsFixedFormat 创建了一些 PDF,但随后出错

问题描述

我在 Visual Studio 中的 VB.Net 程序创建 Excel 文件,然后将它们保存为 PDF,但经过这么多(30、40 或其他,不是重点)它出错说“错误 System.Runtime.InteropServices.COMException:'来自 HRESULT 的异常:0x800A03EC'”。可能我认为这是 Excel 关闭/发布的方式?
在我打开 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 进程结束时如何释放/关闭存在问题。我认为这是因为程序运行良好并将 Excel 文件保存为 PDF,但每次它都不会创建所有文件。它在有“ExportAsFixedFormat”的行创建了许多 PDF 文件后停止。它永远不会停留在同一个特定文件上,所以我会说任何特定的 PDF 都没有问题。

标签: excelvb.netpdfvisual-studio-2017

解决方案


我主要用“Printout”更改了“ExportAsFixedFormat”,现在它似乎工作正常。感谢大家的帮助!

这是我现在拥有的代码:

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)

推荐阅读