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.

Uses of Converting Range to Image

Excel has option to protect the sheet from getting edited by unauthorized user. Though, You can use this option & convert the data to image file or PDF file to make it tough for others to edit it.

The other use is that, sometimes if the Excel data is sent as Outlook mail. In that case, users convert Excel to HTML table format & make it mail content. In this methods, sometimes, the alignment is disturbed on the receiver’s end. If this is converted to image and sent as Outlook mail, the format is preserved.

To publish Excel table format in websites or blogs, the data can be converted to image and posted. This would make tough for the data scrapping people to extract data. One problem with this methods is that, the image files will be of huge volume than the actual data. This would increase the website loading time.

Overall, it is a good way to preserve files with the original data.