Solved

VBA to Print from Excel 2010 to pdf to a given path with a given filename.

Posted on 2014-04-02
4
2,817 Views
Last Modified: 2014-04-02
I have an Excel 2010 workbook with 9 worksheets.

7 of the worksheets each contains a report in a named range on it.

I want to print these reports either individually or preferrably together in one pdf and then save to a given directory using VBA. Before creating any pdf I will use VBA to assign a unique name to it.

I normally use PDFCreator for printing to pdf format, but I will use whatever can do my task well.

Do you have a solution please.
0
Comment
Question by:Fritz Paul
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 39972363
It seems that the save-as PDF functionality is built into Excel.
From there, you can choose Options to get the entire workbook printed.  Not sure, but you can probably hide the 2 pages you don't want and they won't get printed.

So here's the approach I'd take:

On request (a button or other event) launch some VBA.  Have the VBA
1) request a file name
2) hide the sheets that you don't want to print
3) save-as PDF with the option for all workbooks
4) unhide the sheets

Does that sound like an approach that will work for you?

(Oh, and it seems that each sheet can have it's own print area that will ignore parts you don't want to print.)
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39972384
the core of the code is here:

   strFileName = inputbox("File path?")
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        strFileName _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True

Open in new window

0
 
LVL 26

Expert Comment

by:MacroShadow
ID: 39972390
The work flow would be:
1. optional - Copy the named ranges to one worksheet
2. Save the worksheet/workbook as PDF (no need for third-party components)
0
 

Author Closing Comment

by:Fritz Paul
ID: 39974212
Thanks that helped a lot.
It is really convenient.
I can select all the sheets I want to print and then just go save them all together in one pdf.
I recorded a macro and that worked fine.
I will figure out the rest of the code.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now