Solved

Generating PDFs of excel charts - automation

Posted on 2013-02-02
6
264 Views
Last Modified: 2013-02-07
I have an Excel work sheet with 20 line charts. Each chart has its own legend.

I want to "print' each chart to a PDF file. (for inclusion into a ID project).  Is there VBA that can do this. The treename of each PDF woud be in a cell near the upper right of each chart.

Thank you,
Don OHara
0
Comment
Question by:donohara1
6 Comments
 
LVL 14

Expert Comment

by:btdownloads7
ID: 38848073
Well, it's technically not a part of VBA, but you can get a 3rd-party program that will do it, and as long as it supports commandline options, you can issue a shell command in VBA that will generate the PDF.

GhostScript is a free program that can do it, and you can run everything from the commandline.
0
 
LVL 2

Expert Comment

by:sg08234
ID: 38848096
My preferred tool is: PDFCreator

Commandline options: see http://www.pdfforge.org/content/command-line-parameters
0
 
LVL 16

Expert Comment

by:terencino
ID: 38848160
If you have Office 2007 or above you can select the chart, then save as PDF to a file name of your choice. Here is some sample code that will cycle through every embedded chart in your worksheet and save to a folder of your choice. Just change the chart_folder. Make sure the chart name is in the cell above the top left corner of the chart, or just modify the code to suit where it is. The chart treename(?) shouldn't include any characters like : ? | / \ etc

Sub Chart_to_PDF()
Dim c As ChartObject
chart_folder = "C:\test\"
For Each c In ActiveSheet.ChartObjects
    chart_file_name = chart_folder & ActiveSheet.Range(c.TopLeftCell.Address).Offset(-1, 0).Value & ".PDF"
    c.Activate
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=chart_file_name
Next c
End Sub

Open in new window

...Terry
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:donohara1
ID: 38849902
Thanks Terry for your response.
I placed the VBA code in my excel workbook (attached). There are 3 charts on the Charts to PDF tab. I executed the macro and the first chart was created (attached), but then I got the error msg box  just after the first chart is sucessfully created. Are there other arguments to the Activesheet. export  function??

Please advise,  thank you very much for your help,

Don
Charts-Russell-MICROIndex-0113XX.xlsx
Error-msg-file.docx
Index1.PDF
0
 
LVL 16

Accepted Solution

by:
terencino earned 500 total points
ID: 38849916
Hi Don, yes that is a minor problem just the location of the chart. The second and third charts are only slightly over the cell with the file name in it, but that is enough to return a blank for the file name, which is causing the error. To align the charts with cells, hold down the Alt key while positioning the chart with your mouse, you will see that it "snaps" into position.

Another option is to use your chart title instead, I have updated the macro to use that here:
Sub Chart_to_PDF()
Dim c As ChartObject
chart_folder = "C:\test\"
For Each c In ActiveSheet.ChartObjects
    chart_file_name = chart_folder & c.Chart.ChartTitle.Text & ".PDF"
    c.Activate
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=chart_file_name
Next c
End Sub

Open in new window

...Terry
0
 

Author Closing Comment

by:donohara1
ID: 38865313
Well done and appreciated.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

856 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