Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50103

Print multiple sheets to one PDF using PDFCreator

$
0
0
Hi. I have a workbook with a userform on each page that has a list of all the worksheets that would normally be printed. (Some of the worksheets you would not print so not every worksheet is listed on the userform). The worksheets are listed in checkboxes and the user would check which sheets they want to print. There is then a textbox beside each checkbox with a quantity option for them to print. So if they want 2 of 1 sheet and 1 of the other they can do that. I have the code working fine for printing to an actual printer but i cant get the option to print all selections to a single pdf to work properly. Sometimes when i check the boxes i want to save as a pdf it will go through properly and i can just use the wait-collect button in the print que for PDFCreator but 80% of the time nothing show sup at all. not sure what the issue is there.

Here is a copy of the working code that prints to a physical printer:

Code:

Private Sub PrintButton_Click()

Dim ReportPgs
Dim GraphPgs
Dim AnnualPaperPgs
Dim SMPgs
Dim BIMPgs
Dim SOPgs
Dim DUSPgs

ReportPgs = TextBox1.Value
GraphPgs = TextBox2.Value
AnnualPaperPgs = TextBox3.Value
SMPgs = TextBox4.Value
BIMPgs = TextBox5.Value
SOPgs = TextBox6.Value
DUSPgs = TextBox7.Value


If TextBox1.Value < 1 Or TextBox1.Value = "" Then ReportPgs = 1
If TextBox2.Value < 1 Or TextBox2.Value = "" Then GraphPgs = 1
If TextBox3.Value < 1 Or TextBox3.Value = "" Then AnnualPaperPgs = 1
If TextBox4.Value < 1 Or TextBox4.Value = "" Then SMPgs = 1
If TextBox5.Value < 1 Or TextBox5.Value = "" Then BIMPgs = 1
If TextBox6.Value < 1 Or TextBox6.Value = "" Then SOPgs = 1
If TextBox7.Value < 1 Or TextBox7.Value = "" Then DUSPgs = 1


PrintButton.Caption = "Print"
If CheckBox1.Value = True Then Sheets("Daily Report").PrintOut copies:=ReportPgs, Collate:=True
If CheckBox2.Value = True Then Sheets("Graph").PrintOut copies:=GraphPgs, Collate:=True
If CheckBox3.Value = True Then Sheets("Annual Checklist Paper Copy").PrintOut copies:=AnnualPaperPgs, Collate:=True
If CheckBox4.Value = True Then Sheets("Service Module").PrintOut copies:=SMPgs, Collate:=True
If CheckBox5.Value = True Then Sheets("Bolt Inspection Module").PrintOut copies:=BIMPgs, Collate:=True
If CheckBox6.Value = True Then Sheets("Site Overview").PrintOut copies:=SOPgs, Collate:=True
If CheckBox7.Value = True Then Sheets("Detailed Unit Summary").PrintOut copies:=DUSPgs, Collate:=True
End sub

And here is the code i am looking at trying to adapt into the above somehow to print to pdf.

Code:

Option Explicit
Private Sub PDFButton_Click()


Sub PrintToPDF_MultiSheetToOne_Late()
'Author      : Ken Puls (www.excelguru.ca)
'Macro Purpose: Print to PDF file using PDFCreator
'  (Download from http://sourceforge.net/projects/pdfcreator/)
'  Designed for late bind, no references req'd
    Dim pdfjob As Object
    Dim sPDFName As String
    Dim sPDFPath As String
    Dim lSheet As Long
    Dim lTtlSheets As Long

    '/// Change the output file name here!  ///
    sPDFName = "Consolidated.pdf"
    sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
    Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")

    'Make sure the PDF printer can start
    If pdfjob.cStart("/NoProcessingAtStartup") = False Then
        MsgBox "Can't initialize PDFCreator.", vbCritical + _
            vbOKOnly, "Error!"
        Exit Sub
    End If

    'Set all defaults
    With pdfjob
  w .cOption("UseAutosave") = 1
        .cOption("UseAutosaveDirectory") = 1
        .cOption("AutosaveDirectory") = sPDFPath
        .cOption("AutosaveFilename") = sPDFName
        .cOption("AutosaveFormat") = 0    ' 0 = PDF
        .cClearCache
    End With

    'Print the document to PDF
    lTtlSheets = Application.Sheets.Count
    For lSheet = 1 To Application.Sheets.Count
        On Error Resume Next 'To deal with chart sheets
        If Not IsEmpty(Application.Sheets(lSheet).UsedRange) Then
            Application.Sheets(lSheet).PrintOut copies:=1, ActivePrinter:="PDFCreator"
        Else
            lTtlSheets = lTtlSheets - 1
        End If
        On Error GoTo 0
    Next lSheet

    'Wait until all print jobs have entered the print queue
    Do Until pdfjob.cCountOfPrintjobs = lTtlSheets
        DoEvents
    Loop

    'Combine all PDFs into a single file and start the printer
    With pdfjob
        .cCombineAll
        .cPrinterStop = False
    End With

    'Wait until the file shows up before closing PDF Creator
    Do
        DoEvents
    Loop Until Dir(sPDFPath & sPDFName) = sPDFName

    pdfjob.cClose
    Set pdfjob = Nothing


End Sub

Any help is appreciated

Viewing all articles
Browse latest Browse all 50103

Trending Articles