Just recently my company has migrated everyone from Windows XP to Windows7. We are now noticing random crashes when printing reports to PDF. There does not seem to be any pattern to the crashing. I can run a process a dozen times successfully and then later on it will crash. Every time is craches it seems to crash on a different report, or when I'm combining 2 PDFs together.
I have tried a number of ways or re-writing the code, but due to the randomness of the crashing it is very difficult to establish if the changes work.
Has anyone else experinced the same thing with Windows7?
Here is an example of the code
Code: Select all
Function PackageOutput(arrayOutput As Variant, strPDFFileName As String, rptBlankPage As String, Optional booReplace As Boolean = False, Optional booEmbedFont As Boolean = True) As Integer
' --- arrayOutput is the list of documents/reports in the correct order
'     The first part is the document type
'     The second part is the name
'     This sub-routine only handles Access reports and PDF files
'        (but could be adjusted to Word docs as well)
    On Error GoTo PackageOutput_Errors
    PackageOutput = 3 'Set the output to show a Fail - A successful run through will change this to Success
    
' --- Initialize the PDF printer
    Dim pdfPrinter As Object ', strPwd As String, strUsr As String
    Dim pdfDoc As Object
    Dim d As Integer, booAbort As Boolean, numPGs As Integer
    Dim PDFset As PDF_Settings
    Dim strFileName As String
    Dim FSO As Object
    Dim fnOptions As Variant, strLockName As String
    Dim booCont As Boolean
    
    strFileName = "m:\PDFTemp.pdf"
    booCont = False
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set pdfPrinter = MyAmyuni  'AmyuniPrinter 'CreateObject("CDIntfEx.CDIntfEx.4.5")
    Set pdfDoc = AmyuniDocument 'CreateObject("CDIntfEx.document.4.5")
        
' --- Store current settings
    PDFset.PwdOwner = pdfPrinter.OwnerPassword
    PDFset.PwdUser = pdfPrinter.UserPassword
    PDFset.Encrypt = pdfPrinter.encryption
    PDFset.Encrypt_Permissions = pdfPrinter.Permissions
' --- Configures printer
    ' Embed Fonts - if required
    If booEmbedFont Then
        pdfPrinter.fontembedding = True
        pdfPrinter.FileNameOptionsEx = NoPrompt + UseFileName + Concatenate + EmbedFonts + EmbedStandardFonts + FullEmbed
        fnOptions = NoPrompt + UseFileName + Concatenate + EmbedFonts + EmbedStandardFonts + FullEmbed
    Else
        pdfPrinter.FileNameOptionsEx = NoPrompt + UseFileName + Concatenate
        fnOptions = NoPrompt + UseFileName + Concatenate
    End If
    
    ' Remove encryption
    pdfPrinter.Permissions = 0
    pdfPrinter.OwnerPassword = ""
    pdfPrinter.UserPassword = ""
    pdfPrinter.encryption = False
'<<<   Now moved outside this sub routine   >>>    
'' --- Make Amyuni the default printer
'    pdfPrinter.SetDefaultPrinter
' --- Delete the temp PDF file if it already exists
    If FSO.FileExists(strFileName) Then
        Kill strFileName
    End If
' --- Send documents/reports to the PDF output file
    For d = LBound(arrayOutput, 2) To UBound(arrayOutput, 2)
        Select Case arrayOutput(LBound(arrayOutput, 1), d)
            Case "pdf", "doc"
                Call pcProgress(CLng(d), UBound(arrayOutput, 2), "Adding insert: " & getFileName(CStr(arrayOutput(UBound(arrayOutput, 1), d))))
            Case Else
                Call pcProgress(CLng(d), UBound(arrayOutput, 2), "         Compiling disclosure")
        End Select
        'First add Blank page if PDF has Odd number of pages
        If FileExists(strFileName) Then
            pdfDoc.Open strFileName
            
            If numPGs < pdfDoc.pagecount Then
                'Last print was added to the file - so OK to continue
                numPGs = pdfDoc.pagecount
            Else
                'Last print failed - so fail the process
                If booCont Then
                    booCont = False
                Else
                    GoTo PackageOutput_Exit
                End If
            End If
            
            If numPGs Mod 2 = 1 Then 'pdfDoc.pagecount Mod 2 = 1 Then
            ' --- Lock the PDF printer before printing - allows for file name and file name options to be passed at this point
                strLockName = rptBlankPage
                pdfPrinter.lock strLockName
                pdfPrinter.SetDocFileProps strLockName, fnOptions, "", strFileName
            ' --- Print the report to the PDF
                DoCmd.OpenReport rptBlankPage, acViewNormal
                
                numPGs = numPGs + 1
            End If
        End If
    ' --- Now add the reports or PDF
        On Error GoTo PackageOutput_Exit 
        Select Case arrayOutput(LBound(arrayOutput, 1), d)
            Case "pdf"
            ' --- Do a check to see if file exists
                If FileExists(CStr(arrayOutput(UBound(arrayOutput, 1), d))) Then
                    DoEvents
                    Call Append_2_PDF_docs(strFileName, CStr(arrayOutput(UBound(arrayOutput, 1), d)))
                Else
                    If MsgBox("Cannot find: " & CStr(arrayOutput(UBound(arrayOutput, 1), d)) & vbCrLf & "Do you wish to continue? ", vbYesNo + vbExclamation, " !!! Missing PDF insert !!!") = vbNo Then
                        GoTo PackageOutput_Exit
                    End If
                    booCont = True
                End If
            Case "rpt"
            ' --- Lock the PDF printer before printing - allows for file name and file name options to be passed at this point
                strLockName = arrayOutput(UBound(arrayOutput, 1), d)
                pdfPrinter.lock strLockName
                pdfPrinter.SetDocFileProps strLockName, fnOptions, "", strFileName
                
            ' --- Print the report to the PDF
                DoCmd.OpenReport arrayOutput(UBound(arrayOutput, 1), d), acViewNormal
                DoEvents
                
        End Select
        DoEvents
    Next d
    
    ' --- Check to see if the output has completed correctly - a CAT file will remain if there was a problem
    If Not FSO.FileExists(strFileName & ".cat") Then
        Call pcProgress(1, 2, "  Removing temporary file ... ")
        If FSO.FileExists(strPDFFileName) Then
            Kill strPDFFileName
        End If
        Call pcProgress(2, 2, "    Creating output file ... ")
    ' --- Move the temporary file to the correct output location once it is complete
        FSO.moveFile strFileName, strPDFFileName
        ' Process successful - can show the check mark on the form
        PackageOutput = 2
    End If
    
    DoEvents
    
PackageOutput_Exit:
' --- Unlock in case the printing fails
    pdfPrinter.unlock strLockName, 1000 
' --- Restore pervious configuration
    pdfPrinter.encryption = PDFset.Encrypt
    pdfPrinter.OwnerPassword = PDFset.PwdOwner
    pdfPrinter.UserPassword = PDFset.PwdUser
    pdfPrinter.Permissions = PDFset.Encrypt_Permissions
    pdfPrinter.DefaultFileName = "" 
    
    ' Remove Font embedding if added
    If booEmbedFont Then
        pdfPrinter.fontembedding = False
    End If
    
'<<<   Now moved outside this sub routine   >>>
'' --- Restore the previous default printer
'    Call pcProgress(1, 2, "    Restoring default printer ")
'    pdfPrinter.RestoreDefaultPrinter
    
' --- Finish the job and tidy up
'    pdfPrinter.DriverEnd
'<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>
    Call pcProgress(0, 0)
    Set FSO = Nothing
    Set pdfPrinter = Nothing
    Set pdfDoc = Nothing
    
    Exit Function
PackageOutput_Errors:
    Select Case Err.Number
        Case 70 'Permission denied - when trying to delete the PDF
            MsgBox "The temporary compilation file is open or locked.  Please close down and try to delete the file manually", vbInformation + vbOKOnly, " !!! Cannot delete file !!!"
        Case Else
            Call OpenMessage("sub: PackageOutput")
    End Select
    'Resume
    GoTo PackageOutput_Exit
End Function