troubleshooting Question

VB.Net convert Excel file to PDF: error at ExportAsFixedFormat

Avatar of jwebster77
jwebster77 asked on
Microsoft OfficeMicrosoft ExcelVisual Basic.NET
5 Comments1 Solution36 ViewsLast Modified:
ERROR MESSAGE: System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'

I narrowed down my problem but I still get an error:
I have a loop that opens up each excel file from a location and then saves it as PDF.  It works and it creates a number of PDFs and then errors out at the ExportAsFixedFormat line.  The number of PDFs files that get created varies, therefore I would not think there is a problem with the Excel file.

Also, I added DoEvents and Wait(hopefully in the right spots) but I still get the error.  

I also noticed that after I get the error I move the debugger back a few lines and run again the ExportAsFixedFormat and it runs fine. It creates that PDF and continues to produce more.


Please help as I have been trying to find a solution for days.
This is the code I have now:

'OPEN XLSX DOC TO SAVE AS PDF
Dim xl As Object
xl = CreateObject("Excel.Application")
Dim xwb As Object
Dim workBooks = xl.Workbooks


System.Windows.Forms.Application.DoEvents()


xwb = workBooks.Open("\\ken-resourcesan\fileshares\fieldshare\IT\nsantagata\ARStatements_CustomerInvoicesExcel\" & originalCustomerName & " " & customerNumber & " 050720.xlsx")

xwb.ActiveSheet.PageSetup.Zoom = False
xwb.ActiveSheet.PageSetup.FitToPagesWide = 1
xwb.ActiveSheet.PageSetup.FitToPagesTall = False

           
NewDate = DateAndTime.Now.AddSeconds(10)
Do Until DateAndTime.Now > NewDate
     Application.DoEvents()
Loop


Dim newHour = Hour(Now())
Dim newMinute = Minute(Now())
Dim newSecond = Second(Now()) + 10
Dim waitTime = TimeSerial(newHour, newMinute, newSecond)
xl.Wait(waitTime)


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


xwb.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xwb)
xwb = Nothing
workBooks.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBooks)
workBooks = Nothing
xl.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl)
xl = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros