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

问题描述 投票:0回答:1

我在Visual Studio中的VB.Net程序创建Excel文件,然后将它们另存为PDF,但是经过很多(30、40或其他,不是重点)之后,它错误地提示“错误System.Runtime.InteropServices.COMException:'Exception来自HRESULT:0x800A03EC'”。可能我在想这是Excel被关闭/发布的方式吗?打开Excel文档并尝试将其另存为PDF后,错误是来自此行代码。同样,这总是在某些文档已经保存为PDF(有时30,有时是50 ...)之后发生:

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

这是我的全部代码:

这将创建出色的文档并将其填充数据:

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都没有问题。

excel vb.net pdf visual-studio-2017
1个回答
0
投票

我主要是将“ ExportAsFixedFormat”更改为“ Printout”,现在看来工作正常。感谢大家的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.