?
Solved

Generating PDFs of excel charts - automation

Posted on 2013-02-02
6
Medium Priority
?
269 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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 create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

770 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