VBA to print to default printer and pdf simultaneously

Dan5977

Board Regular
Joined
Aug 7, 2008
Messages
111
Office Version
  1. 2010
Hi,

I have a little script which prints out a selection of pages from a workbook to my default printer.

Code:
Private Sub CommandButton1_Click()
    Dim WS As Worksheet
    For Each WS In ActiveWorkbook.Worksheets
    If WS.Range("$A$1") = "Print" Then WS.PrintOut
      Next WS
End Sub

Different pages will print depending on the parameters of the job, so I have formulas in cell A1 of each sheet to show whether that page is to be printed or not. This all works fine.

I now want to extend the functionality so that at the same time I can create and save a single PDF file containing all of the pages with "Print" in cell A1 in order. Ideally the name of this pdf will also be set to 'Inputs!B3&"_mfg_dwg_pk" - our job no. is located on the 'Inputs' tab in cell B3, so a typical name may be 40850_mfg_dwg_pk. If this is not easily done then we can of course name the PDF when prompted by the PDF software.

Any assistance will, as always, be appreciated. Thanks in advance :)
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I'm still stuck with this :(

The spreadsheet should already be saved in the correct place, so ideally the pdf will be created, named and saved in the same folder as the spreadsheet.

I would really appreciate any help.
 
Upvote 0
Thanks John!

I thought I had searched thoroughly but obviously not. Apologies for that and thanks for expanding on it with the file name code. I haven't tried it yet but I'm confident it will work.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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