Solved

Generating PDFs of excel charts - automation

Posted on 2013-02-02
6
260 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel 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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

803 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