Search notes:

Excel: Save a range as image

The following example demonstrates range.copyPicture.
It was inspired by (and mostly copied from) this Stackoverflow answer.
option explicit

sub main() ' {

    cells(1, 1) = "Number" : cells(1,2) =  42
    cells(2, 1) = "Text"   : cells(2,2) = "Hello world"

    activeSheet.usedRange.columns.autoFit

    saveRangeAsImage _
       range(cells(1,1), cells(2,2)), _
       environ("temp") & "\exportedRange.png"

end sub ' }

sub saveRangeAsImage(rng as range, fileName as string) ' {

    rng.copyPicture               _
        appearance := xlScreen  , _
        format     := xlPicture

    dim crt as chartObject
    set crt = rng.parent.chartObjects.add(0, 0, rng.width, rng.height)

  '
  ' These doEvents seem necessary to give some time to Excel
  ' to complete some operation
  '
    doEvents
    doEvents
    doEvents

    crt.chart.paste
    crt.chart.export filename := fileName, filtername := "png"

    crt.delete

end sub ' }
Github repository about-MS-Office-object-model, path: /Excel/Range/copyPicture/saveRangeAsImage.vb

See also

chart.export()
chartObject.copyPicture

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Microsof...', 1745614926, '3.148.236.97', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Microsoft/Office/Excel/Object-Model/Range/copyPicture/save-range-as-image(84): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78