Export Excel Range to Image (jpg/png/gif) using Vba?
This code ill convert the data in Excel workbook into a jpg image file as how it is displayed on screen. The image format can be jpg, png or gif. We have not tested if this supports other image formats as well, but you can try it. Related Post: Convert Excel to PDF file. The output image file will be saved in the same path as the Excel workbook. Change the path if You want to save the image file in a different location.Sub ExcelToJPGImage(imageRng As Range)
'Code from officetricks.com
Dim sImageFilePath As String
sImageFilePath = ThisWorkbook.Path & Application.PathSeparator & "ExcelRangeToImage_"
sImageFilePath = sImageFilePath & VBA.Format(VBA.Now, "DD_MMM_YY_HH_MM_SS_AM/PM") & ".jpg"
'Create Temporary workbook to hold image
Dim wbTemp As Workbook
Set wbTemp = Workbooks.Add(1)
'Copy image & Save to new file
imageRng.CopyPicture xlScreen, xlPicture
wbTemp.Activate
With wbTemp.Worksheets("Sheet1").ChartObjects.Add(imageRng.Left, imageRng.Top, imageRng.Width, imageRng.Height)
.Activate
.Chart.Paste
.Chart.Export Filename:=sImageFilePath, FilterName:="jpg"
End With
'Close Temp workbook
wbTemp.Close False
Set wbTemp = Nothing
MsgBox "Image File Saved To: " & sImageFilePath
End Sub
There is no direction function to save Excel range as jpg image file. But there is function to export a Chart to image file. We are actually tricking this for our purpose.
External Reference: Microsoft article that explains more about this function.