Results 1 to 2 of 2

Thread: [RESOLVED] VB.Net convert Excel file to PDF: error at ExportAsFixedFormat

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2016
    Posts
    17

    Resolved [RESOLVED] VB.Net convert Excel file to PDF: error at ExportAsFixedFormat

    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:

    Code:
    '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()
    Last edited by NicoVB.Net; May 11th, 2020 at 08:32 AM.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jan 2016
    Posts
    17

    Re: [RESOLVED] VB.Net convert Excel file to PDF: error at ExportAsFixedFormat

    I mainly changed "ExportAsFixedFormat" with "Printout" and now it seems to be working fine. Thanks to all for your help!

    THIS IS THE CODE i HAVE NOW:

    Code:
    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)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width