As you might know, I'm a newbie working with VBA code. I found Ken Puls's excellent macro on printing specified excel worksheets to pdf format using PDFCreator.
What I'm trying to do is adapt the code to do the two following things. Please answer one or both of the questions asap because I'm up against a deadline!
1. This is the easy one. I am trying to imbed the code below as a CommandButton control into #2 and don't know how to do it. I keep getting error messages on #2 coding even without adding this code. I'll add it once I get #1 running.
Private Sub CommandButton1_Click()
ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:="1"
With ActiveSheet.PageSetup
'Here I want to add different ranges for different worksheets:
.PrintArea = "A1:K32" 'And add for a second worksheet, A1:F36, etc.
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
End With
ActiveSheet.PrintOut ActivePrinter:="PDFCreator"
End Sub
2. I want to add different ranges for different worksheets within the same workbook. Here is a bit of Ken's code:
'Print Multiple Worksheets to a Single PDF File:
Option Explicit
Sub PrintToPDF_MultiSheetToOne_Early()
'Author : Ken Puls (www.excelguru.ca)
'Macro Purpose: Print to PDF file using PDFCreator
' (Download from http://sourceforge.net/projects/pdfcreator/)
' Designed for early bind, set reference to PDFCreator
_____
and so on until:
'Assign settings for PDF job
With pdfjob
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Delete the PDF if it already exists
If Dir(sPDFPath & sPDFName) = sPDFName Then Kill (sPDFPath & sPDFName)
'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 EarlyExit
Next lSheet
'and so on till
End Sub
Thanks to all for any help you can offer!
What I'm trying to do is adapt the code to do the two following things. Please answer one or both of the questions asap because I'm up against a deadline!
1. This is the easy one. I am trying to imbed the code below as a CommandButton control into #2 and don't know how to do it. I keep getting error messages on #2 coding even without adding this code. I'll add it once I get #1 running.
Private Sub CommandButton1_Click()
ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:="1"
With ActiveSheet.PageSetup
'Here I want to add different ranges for different worksheets:
.PrintArea = "A1:K32" 'And add for a second worksheet, A1:F36, etc.
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
End With
ActiveSheet.PrintOut ActivePrinter:="PDFCreator"
End Sub
2. I want to add different ranges for different worksheets within the same workbook. Here is a bit of Ken's code:
'Print Multiple Worksheets to a Single PDF File:
Option Explicit
Sub PrintToPDF_MultiSheetToOne_Early()
'Author : Ken Puls (www.excelguru.ca)
'Macro Purpose: Print to PDF file using PDFCreator
' (Download from http://sourceforge.net/projects/pdfcreator/)
' Designed for early bind, set reference to PDFCreator
_____
and so on until:
'Assign settings for PDF job
With pdfjob
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Delete the PDF if it already exists
If Dir(sPDFPath & sPDFName) = sPDFName Then Kill (sPDFPath & sPDFName)
'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 EarlyExit
Next lSheet
'and so on till
End Sub
Thanks to all for any help you can offer!
Last edited: