Results 1 to 6 of 6

Thread: PDFCreator generates corrupt PDF file

  1. #1

    Unhappy PDFCreator generates corrupt PDF file

    Register for a FREE account, and/
    or Log in to avoid these ads!

    Hi @ all

    I've created a windows forms application which should create a pdf out of specific excel file with selected worksheets! Unfortunately, the code below only generates corrupt PDF files and I can't see the reason: "Adobe Reader could not open 'Document.pdf' because it is either not a supported file type or because the file has been damaged..."

    I've already asked in the forum, but there seems to be nobody who can help! Why is this code not working???

    Here's the situation:
    - PDFCreator 1.2.3
    - MS Excel 2010 / MS Office 2010
    - Visual Studio Professional 2010

    ...and here's the modified code from Ken Puls (VBA to VB.NET):

        Public Sub MainXLS()
            ' Declare variables
            Dim ExcelInstance As New Microsoft.Office.Interop.Excel.Application
            Dim ExcelWorkbook As Microsoft.Office.Interop.Excel.Workbook
            ' Hide excel instance
            ExcelInstance.Visible = False
            ' Open workbook
            ExcelWorkbook = ExcelInstance.Workbooks.Open("C:\Users\ad\Desktop\411539_XXXXXX_CAM.XLS")
            ' Create PDF
            PrintToPDF_SpecifiedSheetsToOne_Early(ExcelWorkbook, _
                                                  ExcelInstance, _
                                                  "DECKBLAT.XLU,DECKBLAT.XLS,SPEZ_SW.XLS", _
                                                  "C:\Users\ad\Desktop", _
            ' Close workbook and Excel instance
            ' Clean up
            ExcelWorkbook = Nothing
            ExcelInstance = Nothing
        End Sub
        Private Sub PrintToPDF_SpecifiedSheetsToOne_Early(ByRef ExcelWorkbook As Microsoft.Office.Interop.Excel.Workbook, _
                                                          ByRef ExcelInstance As Microsoft.Office.Interop.Excel.Application, _
                                                          ByVal SheetSelection As String, _
                                                          ByVal OutputPath As String, _
                                                          ByVal OutputName As String)
            '   Author       : Ken Puls (
            '   Modified by  : Olphi
            '   Macro Purpose: Print to PDF file using PDFCreator
            '   (Download from
            '   Designed for early bind, set reference to PDFCreator
            ' Declare variables
            Dim pdfJob As PDFCreator.clsPDFCreator
            Dim strPDFName As String
            Dim strPDFPath As String
            Dim strSheets() As String
            Dim boolRestart As Boolean
            Dim longSheet As Long
            Dim longTotalSheets As Long
                ' Define the output 
                strPDFName = OutputName & ".pdf"
                strPDFPath = OutputPath & ExcelInstance.PathSeparator
                ' Split the sheets into an array
                strSheets = SheetSelection.Split(",")
                ' Turn off screen updates
                ExcelInstance.ScreenUpdating = False
                ' Create new PDFCreator instance
                pdfJob = New PDFCreator.clsPDFCreator
                ' Check if PDFCreator is already running and attempt to kill the process if so
                    boolRestart = False
                    pdfJob = New PDFCreator.clsPDFCreator
                    If pdfJob.cStart("/NoProcessingAtStartup") = False Then
                        'PDF Creator is already running, kill the existing process
                        Shell("taskkill /f /im PDFCreator.exe", vbHide)
                        pdfJob = Nothing
                        boolRestart = True
                    End If
                Loop Until boolRestart = False
                'Assign settings for PDF job
                With pdfJob
                    .cVisible = False
                    .cOption("UseAutosave") = 1
                    .cOption("UseAutosaveDirectory") = 1
                    .cOption("AutosaveDirectory") = strPDFPath
                    .cOption("AutosaveFilename") = strPDFName
                    .cOption("AutosaveFormat") = 0    ' 0 = PDF
                End With
                ' Delete the PDF if it already exists
                If Dir(strPDFPath & strPDFName) = strPDFName Then Kill(strPDFPath & strPDFName)
                'Print the document to PDF
                For longSheet = LBound(strSheets) To UBound(strSheets)
                    ' Check if there is printable data inside the selected worksheet
                    If Not IsNothing(ExcelInstance.Sheets(strSheets(longSheet)).UsedRange) Then
                        ' Print worksheet
                        ExcelInstance.Sheets(strSheets(longSheet)).PrintOut(copies:=1, ActivePrinter:="PDFCreator auf NE01:")
                        ' Increase sheet counter
                        longTotalSheets = longTotalSheets + 1
                    End If
                Next longSheet
                ' Wait until all print jobs have entered the print queue
                Do Until pdfJob.cCountOfPrintjobs = longTotalSheets
                ' Combine all PDFs into a single file and stop the printer
                With pdfJob
                    .cPrinterStop = False
                End With
                ' Wait until the file shows up before closing PDF Creator
                Loop Until Dir(strPDFPath & strPDFName) = strPDFName
            Catch ex As Exception
                'Inform user of error, and go to cleanup section
                MsgBox("There was an error encountered.  PDFCreator has" & vbCrLf & _
                       "has been terminated.  Please try again.", _
                       vbCritical + vbOKOnly, "Error")
                'Release objects and terminate PDFCreator
                pdfJob = Nothing
                Shell("taskkill /f /im PDFCreator.exe", vbHide)
                ' Turn on screen updates
                ExcelInstance.ScreenUpdating = True
            End Try
        End Sub

  2. #2
    Acolyte maninweb's Avatar
    Join Date
    Mar 2011
    Excel Version
    2010, 2016, 2016 Insider
    Hi Olphi...

    I don't know, if this helps, as I am not a specialist for .Net. But if I understand the code right, are you passing workbook names to the function (like SPEZ_SW.XLS)?
    I can also suggest to try to manually print an Excel document using the PDF Creator. If this also leads to a corrupted document, then I think, there may be a problem with the installation.

    Best Regards :-)

  3. #3


    Hi and thank you for your time!

    Yes, manually it always works, so the problem lies in the code i guess! Yes the workbook names are passing to the printer by this code:
    strSheets = SheetSelection.Split(",")
    The printer prints the files and shows up a file, but the file can't be opened! So I can see how the printer prints all pages within the specified sheets...! Anybody out there who can see it?

    Cheers, Olphi

  4. #4
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Nanaimo, BC, Canada
    Blog Entries
    Excel Version
    Excel Office 365 Insider
    Hi Olphi,

    I'm not really a .NET guy either, but I've spent a ton of time working with PDFCreator... not guaranteeing I can help/solve this mind you, but I'll give it a go.

    Have you tried printing a single worksheet to a PDF via code to see if that works? I don't think that the splitting of the array should be an issue, but I'm thinking we might as well try to start somewhere...
    Ken Puls, FCPA, FCMA, MS MVP

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training), with my book M is for Data Monkey, or our new Power Query Recipe cards!

    Main Site: -||- Blog: -||- Forums:
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

  5. #5

  6. #6
    Seeker SilverChat's Avatar
    Join Date
    Feb 2015
    Excel Version

Posting Permissions

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