VBA Code_Print to PDF

kgallego

Board Regular
Joined
Jul 26, 2011
Messages
82
Office Version
  1. 365
Hello all,

I have a code that prints an active sheet (wsA) to PDF. I want to alter this code to print multiple worksheets (wsB, wsC, wcD, etc.) into the same pdf.

My plan is to set each wsX to their respective worksheets, but I'm not sure where to go after that. Examplt

Dim wsA as worksheet
Dim wsB as worksheet
Dim wsC as worksheet
Set wsA = Sheet1
Set wsB = Sheet2
Set wsC = Sheet3

Below is the code that I'm working with. Any thoughts?

Sub PDFActiveSheet()
'www.contextures.com
'for Excel 2010 and later

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler


Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")


'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & ""


'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")


'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile


'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")


'export to PDF if a folder was selected
If myFile <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If


exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub







Thanks,
Kelsey
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Also,

There is alot of code in there that goes into the naming of the file. If it's possible to alter or eliminate the code and just name it as "Final PDF" that would be great also.

Thanks,
Kelsey
 
Upvote 0
It would be much easier for you to go through all the worksheets by
Code:
Dim ws as Worksheet
set ws = ActiveWorksheet
For each ws in Workbooks("[B]NAME[/B]")
'Your code
Next

With this, you can use variable ws to give the file the exact name as the worksheet
 
Last edited:
Upvote 0
Thanks fine on the naming, but how do I tell the code to print wsA, wsB, wsC, all together as one PDF? Is there a code that will combine them all?
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,484
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top