Solved

Generating PDFs of excel charts - automation

Posted on 2013-02-02
6
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

717 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