Solved

Generating PDFs of excel charts - automation

Posted on 2013-02-02
6
256 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
Comment Utility
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
Comment Utility
My preferred tool is: PDFCreator

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

Expert Comment

by:terencino
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:donohara1
Comment Utility
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
Comment Utility
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
Comment Utility
Well done and appreciated.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Indirect formula 9 28
Dynamic Excel Countdown Graphic 21 15
onOpen 14 27
Excel Array formula issues 4 7
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

762 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

6 Experts available now in Live!

Get 1:1 Help Now