+ Reply to Thread
Results 1 to 2 of 2

Printing Multiple Sheets to One PDF file

Hybrid View

johnnywinter Printing Multiple Sheets to... 08-31-2007, 11:39 PM
FrankBoston One function calls another,... 09-01-2007, 11:03 AM
  1. #1
    Registered User
    Join Date
    08-21-2007
    Posts
    90

    Printing Multiple Sheets to One PDF file

    I have code to print out selected sheets from one workbook. Have it and it works great thanks to leahProton.

    I now need to save those selected mulitple sheets as a single PDF file, so I found code to do that. (opver all problem is to spool all printing s a single job)

    I can get each code to work separately, but not together.

    I need some help, please!

    Thanks, J



    Here is my code to printout the selected sheets"
    Sub report()
    '----------------------------------------------------------------------------------------------------
    'When this procedure is run, it will:
    '    select the QUOTE sheet
    '    count the number of "Items"
    '    lookup the item number on the summary sheet and copy the corresponding sheet name to the printlist array
    '* added:
    '    it will check quote sheet row 33 for an entry, if true, then add the INDIVIDUAL PRODUCT sheet
    '
    '    it will add the QUOTE sheet as the last sheet in the list to be printed.
    '------------------------------------------------------------------------------------------------------
    
         Dim PrintList()                                                       'array to hold sheets to print
         Dim SheetCount As Integer
         Dim ItemCount As Integer                                              'number of non-empty cells in Item column, i.e., A17 thru A29
         Dim TableRange                                                        'data on SUMMARY sheet
         Dim Item                                                              'current cell in list of QUOTE "Items"
         Dim ItemList                                                          'the "Item" column itself
         Set ItemList = Range("a17:a29")
         Set TableRange = Worksheets("Summary").Range("A7:G37")
         
         Worksheets("quote").Select
         ItemCount = Application.WorksheetFunction.CountA(Range("a17:a29"))    'get the total number of items listed
         
         ReDim PrintList(1 To ItemCount)                                       'size the sheet list array to hold this many sheet names
         
    'Process the "Items" column:
    'for each item, check if it has a corresponding sheet name, then,
    'if so, copy the sheet name to the print list
    
         For Each Item In ItemList
              If Item.Value > " " Then                                         '<=== note: changed the test value from "" (NULL) to " " (space)
                   SheetCount = SheetCount + 1
                   If Application.WorksheetFunction.VLookup(Item.Value, TableRange, 7) <> "" Then
                        PrintList(SheetCount) = Application.WorksheetFunction.VLookup(Item.Value, TableRange, 7)
                   Else
                        SheetCount = SheetCount - 1
                   End If
              Else
                   Exit For
              End If
         Next Item
         
    '---------------- mod ------------------------------
    'The following if..then..else block will check for entries in row 33 of the quote sheet.  If there are, then it will
    'add the A. INDIVIDUAL PRODUCTS sheet to the list along with the QUOTE sheet; else it just adds the QUOTE sheet.
    '---------------------------------------------------
    
         If Sheets("quote").Range("E33").Value > "0.00" Then
              SheetCount = SheetCount + 2
              ReDim Preserve PrintList(1 To SheetCount)
              PrintList(SheetCount - 1) = "A. INDIVIDUAL PRODUCTS"
              PrintList(SheetCount) = "QUOTE"
         Else
              SheetCount = SheetCount + 1
              ReDim Preserve PrintList(1 To SheetCount)
              PrintList(SheetCount) = "quote"
         End If
         
         Sheets(PrintList()).Select                             'select all sheets in the list
         ActiveWindow.SelectedSheets.PrintOut Copies:=1         'print the sheets
         Sheets("QUOTE").Select
    End Sub

    AND HERE IS THE CODE TO SAVE MULTIPLE SHEETS AS A SINGLE PDF FILE

      
    Option Explicit
    
    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
            .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 stop the printer
        With pdfjob
            .cCombineAll
            .cPrinterStop = False
        End With
    
        'Wait until PDF creator is finished then release the objects
        Do Until pdfjob.cCountOfPrintjobs = 0
            DoEvents
        Loop
        pdfjob.cClose
        Set pdfjob = Nothing
    End Sub

  2. #2
    Forum Contributor
    Join Date
    07-05-2007
    Location
    Lexington, MA
    Posts
    302

    One function calls another, with sheets selected

    I have made changes without testing to show an approach. Changed lines are marked with Change9
    Option Explicit
    
    '------------------------------------
    Sub report()
    'When this procedure is run, it will:
    '    select the QUOTE sheet
    '    count the number of "Items"
    '    lookup the item number on the summary sheet and copy the corresponding sheet name to the printlist array
    '* added:
    '    it will check quote sheet row 33 for an entry, if true, then add the INDIVIDUAL PRODUCT sheet
    '
    '    it will add the QUOTE sheet as the last sheet in the list to be printed.
    '------------------------------------------------------------------------------------------------------
    
         Dim PrintList()                                                       'array to hold sheets to print
         Dim SheetCount As Integer
         Dim ItemCount As Integer                                              'number of non-empty cells in Item column, i.e., A17 thru A29
         Dim TableRange                                                        'data on SUMMARY sheet
         Dim Item                                                              'current cell in list of QUOTE "Items"
         Dim ItemList                                                          'the "Item" column itself
         Set ItemList = Range("a17:a29")
         Set TableRange = Worksheets("Summary").Range("A7:G37")
         
         Worksheets("quote").Select
         ItemCount = Application.WorksheetFunction.CountA(Range("a17:a29"))    'get the total number of items listed
         
         ReDim PrintList(1 To ItemCount)                                       'size the sheet list array to hold this many sheet names
         
    'Process the "Items" column:
    'for each item, check if it has a corresponding sheet name, then,
    'if so, copy the sheet name to the print list
    
         For Each Item In ItemList
              If Item.Value > " " Then                                         '<=== note: changed the test value from "" (NULL) to " " (space)
                   SheetCount = SheetCount + 1
                   If Application.WorksheetFunction.VLookup(Item.Value, TableRange, 7) <> "" Then
                        PrintList(SheetCount) = Application.WorksheetFunction.VLookup(Item.Value, TableRange, 7)
                   Else
                        SheetCount = SheetCount - 1
                   End If
              Else
                   Exit For
              End If
         Next Item
         
    '---------------- mod ------------------------------
    'The following if..then..else block will check for entries in row 33 of the quote sheet.  If there are, then it will
    'add the A. INDIVIDUAL PRODUCTS sheet to the list along with the QUOTE sheet; else it just adds the QUOTE sheet.
    '---------------------------------------------------
    
         If Sheets("quote").Range("E33").Value > "0.00" Then
              SheetCount = SheetCount + 2
              ReDim Preserve PrintList(1 To SheetCount)
              PrintList(SheetCount - 1) = "A. INDIVIDUAL PRODUCTS"
              PrintList(SheetCount) = "QUOTE"
         Else
              SheetCount = SheetCount + 1
              ReDim Preserve PrintList(1 To SheetCount)
              PrintList(SheetCount) = "quote"
         End If
         
         Sheets(PrintList()).Select                             'select all sheets in the list
         ' ActiveWindow.SelectedSheets.PrintOut Copies:=1       'print the sheets    Change9
         PrintToPDF_MultiSheetToOne_New()                       'Altered function    Change9
         Sheets("QUOTE").Select
    End Sub
    
    '-----------------------
    Sub PrintToPDF_MultiSheetToOne_New()      'Change9  Altered function name
    '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, wsheet as Worksheet  'Change9
        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
            .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             Change9
        lTtlSheets = ActiveWindow.SelectedSheets.Count      Change9
        ' For lSheet = 1 To Application.Sheets.Count        Change9
    
        For Each wsheet in ActiveWindow.SelectedSheets     'Change9
          wsheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
        Next lSheet
    
    '        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
    
        '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 stop the printer
        With pdfjob
            .cCombineAll
            .cPrinterStop = False
        End With
    
        'Wait until PDF creator is finished then release the objects
        Do Until pdfjob.cCountOfPrintjobs = 0
            DoEvents
        Loop
        pdfjob.cClose
        Set pdfjob = Nothing
    End Sub
    FrankBoston is the pen name for Andrew Garland, Lexington MA

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1