I was thinking to include this in the code bank for a long time but didn't think it was that important but after when I replied to one of the threads today, I did a quick search in the forum and found that many members have asked this question in the past (including me) So here it is. I am pasting the code for your reference. Hope it benefits someone... If it does then do leave a comment here
Excel unfortunately doesn't allow you to directly export jpeg, gif images. However It does let you easily export flowcharts, charts etc... so the key is to trick Excel. And we can do that by creating a temp chart and pasting the picture in the chart. We need to work with the dimensions a little bit but after that it very easy to export the image. Here is the code...
I have commented the code so that it would be easy to understand...
'You need to select a picture before running this code
'else it will give you error'
Dim TempChart As String, Picture2Export As String
Dim PicWidth As Long, PicHeight As Long
Picture2Export = Selection.Name
'Store the picture's height and width in a variable
PicHeight = .ShapeRange.Height
PicWidth = .ShapeRange.Width
'Add a temporary chart in sheet1
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
Selection.Border.LineStyle = 0
TempChart = Selection.Name & " " & Split(ActiveChart.Name, " ")(2)
'Change the dimensions of the chart to suit your need
.Width = PicWidth
.Height = PicHeight
'Copy the picture
'Paste the picture in the chart
'Finally export the chart
.ChartObjects(1).Chart.Export Filename:="Sample.jpg", FilterName:="jpg"
'Destroy the chart. You may want to delete it...