PDA

View Full Version : Solved: Re: Exporting An Excel Chart / Range As a Jpeg



lostin_space
07-17-2006, 04:16 AM
Hi all, i've got a sheet where i need to export a chart from excel to powerpoint. i've got code that works fine exporting any object / print area etc to PPT as a bitmap, but, i need to ship out an image, that's a JPEG, to enable me to re-size / scale the object without loosing it's definitions etc

any ideas??

here's my current code :-

Range("Print_Area").CopyPicture Appearance:=xlScreen, Format:=xlBitmap

it's the last element, the 'format:=xlBitmap' that's the issue... i've found examples elsewhere, which say use 'format:=xljpeg', but that just does'nt work!

(i'm using Excel 2k3 btw)

Many thanks in advance

Rgds

Russ
:banghead: :dunno

Justinlabenne
07-17-2006, 04:21 AM
http://www.vbaexpress.com/kb/getarticle.php?kb_id=370

lostin_space
07-17-2006, 04:34 AM
Thanks for the response - i've got something similar to that, which does work, but as i expressed - it only copies over as a bitmap, i need it as a JPEG... any other ideas?

lucas
07-17-2006, 07:01 AM
This entry of Justins shows you how to export as a .gif. I changed it in two places in the code to .jpg and it worked fine. Maybe a start in the right direction for you.
http://vbaexpress.com/kb/getarticle.php?kb_id=449

mdmackillop
07-17-2006, 08:56 AM
Hi Lostin
I was pointed to a free picture utility IrfanView which may be able to handle this. It allows you to run command line instructions from a Shell command.
Regards
MD

jolivanes
07-17-2006, 11:02 PM
lostin_space.

This works for me if this is what you mean.

Sub ExportChartsJPG()
Application.ScreenUpdating = False
Sheets("My_Chart").Select
ActiveChart.Export Filename:="D:\My Documents\My Pics\MyChartA.jpg", _
FilterName:="jpg"
Application.ScreenUpdating = True
HTH
John

mdmackillop
07-17-2006, 11:16 PM
Hi John,
Nice simple solution. Works for me. Try without the knobs and whistles
Sub ExportChartsJPG()
Sheets("My_Chart").Export ("D:\My Documents\My Pics\MyChartA.jpg")
End Sub

Regards
MD

jolivanes
07-18-2006, 01:18 PM
HHi Md
Following works for a range.


Sub ExportNumChart()
Const FName As String = "D:\My Documents\My Pics\Numbers.jpg"
Dim pic_rng As Range
Dim ShTemp As Worksheet
Dim ChTemp As Chart
Dim PicTemp As Picture
Application.ScreenUpdating = False
Set pic_rng = Worksheets("Numbers").Range("B1:F28")
Set ShTemp = Worksheets.Add
Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:=ShTemp.Name
Set ChTemp = ActiveChart
pic_rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
ChTemp.Paste
Set PicTemp = Selection
With ChTemp.Parent
.Width = PicTemp.Width + 8
.Height = PicTemp.Height + 8
End With
ChTemp.Export Filename:="D:\My Documents\My Pics\Numbers.jpg", FilterName:="jpg"
Application.DisplayAlerts = False
ShTemp.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Above was supplied to me by one of the experts on one of the forums. Because Excel is a hobby for me, I rely on people who know what they're doing and who give their time freely. A big thanks to all the "helpers" on these forums.
HTH.
John

mdmackillop
07-19-2006, 11:32 AM
Hi John,
The chart in the sheet produces a small picture. Can you modify this to use a Chart Sheet and then only use the Data area for the JPG? Makes a good KB entry either way.
Regards
MD

lostin_space
07-20-2006, 02:12 AM
cheers all - got it sorted now. thanks much for all ideas / sols & input

Bst Rgds

jolivanes
07-20-2006, 04:41 AM
Hi MD.
I can't remember who it was that generously supplied me with this but it certainly is not my own creation. If I am right, it was on one of the "sister" forums. I use the before mentioned Irfanview to resize the pictures to fit the image box in a userform. As far as a KB entry goes, I would not know where to start but if you are interested, please do.
Good luck
John